Skip to content

Commit 0f76c91

Browse files
committed
Run ShellCheck when server boots with an open file
1 parent c38da48 commit 0f76c91

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

server/src/__tests__/server.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ describe('server', () => {
517517
const { connection, server } = await initializeServer()
518518
const document = FIXTURE_DOCUMENT.COMMENT_DOC
519519

520-
await server.onDocumentContentChange(document)
520+
await server.analyzeAndLintDocument(document)
521521

522522
expect(connection.sendDiagnostics).toHaveBeenCalledTimes(1)
523523
const { diagnostics } = connection.sendDiagnostics.mock.calls[0][0]

server/src/server.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,20 @@ export default class BashServer {
9292
* care about.
9393
*/
9494
public register(connection: LSP.Connection): void {
95+
const hasConfigurationCapability = !!this.clientCapabilities?.workspace?.configuration
96+
97+
let currentDocument: TextDocument | null = null
98+
let initialized = false // Whether the client finished initializing
99+
95100
this.documents.listen(this.connection)
96-
this.documents.onDidChangeContent(async ({ document }) => {
101+
102+
this.documents.onDidChangeContent(({ document }) => {
97103
// The content of a text document has changed. This event is emitted
98104
// when the text document first opened or when its content has changed.
99-
await this.onDocumentContentChange(document)
105+
currentDocument = document
106+
if (initialized) {
107+
this.analyzeAndLintDocument(document)
108+
}
100109
})
101110

102111
this.documents.onDidClose((event) => {
@@ -114,9 +123,6 @@ export default class BashServer {
114123
connection.onCompletionResolve(this.onCompletionResolve.bind(this))
115124
connection.onCodeAction(this.onCodeAction.bind(this))
116125

117-
const hasConfigurationCapability = !!this.clientCapabilities?.workspace?.configuration
118-
let initialized = false
119-
120126
/**
121127
* The initialized notification is sent from the client to the server after
122128
* the client received the result of the initialize request but before the
@@ -162,6 +168,11 @@ export default class BashServer {
162168
}
163169

164170
initialized = true
171+
if (currentDocument) {
172+
// If we already have a document, analyze it now that we're initialized
173+
// and the linter is ready.
174+
this.analyzeAndLintDocument(currentDocument)
175+
}
165176

166177
// NOTE: we do not block the server initialization on this background analysis.
167178
return { backgroundAnalysisCompleted: this.startBackgroundAnalysis() }
@@ -173,7 +184,7 @@ export default class BashServer {
173184
if (configChanged && initialized) {
174185
this.connection.console.log('Configuration changed')
175186
this.startBackgroundAnalysis()
176-
// TODO: we should trigger the linter again
187+
// TODO: we should trigger the linter again for the current file
177188
}
178189
})
179190

@@ -211,10 +222,9 @@ export default class BashServer {
211222
}
212223

213224
/**
214-
* The content of a text document has changed. This event is emitted
215-
* when the text document first opened or when its content has changed.
225+
* Analyze and lint the given document.
216226
*/
217-
public async onDocumentContentChange(document: TextDocument) {
227+
public async analyzeAndLintDocument(document: TextDocument) {
218228
const { uri } = document
219229

220230
let diagnostics: LSP.Diagnostic[] = []
@@ -399,7 +409,6 @@ export default class BashServer {
399409
) {
400410
const shellDocumentation = await getShellDocumentation({ word })
401411
if (shellDocumentation) {
402-
// eslint-disable-next-line no-console
403412
return { contents: getMarkdownContent(shellDocumentation) }
404413
}
405414
} else {

0 commit comments

Comments
 (0)