Skip to content

Commit

Permalink
Relints documents when connecting / disconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
ncordon committed Dec 23, 2024
1 parent 706f9ec commit 818fac7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
6 changes: 2 additions & 4 deletions packages/language-server/src/linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validateSyntax } from '@neo4j-cypher/language-support';
import { Neo4jSchemaPoller } from '@neo4j-cypher/schema-poller';
import debounce from 'lodash.debounce';
import { join } from 'path';
import { Diagnostic, TextDocumentChangeEvent } from 'vscode-languageserver';
import { Diagnostic } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import workerpool from 'workerpool';
import { LinterTask, LintWorker } from './lintWorker';
Expand All @@ -15,12 +15,10 @@ const pool = workerpool.pool(join(__dirname, 'lintWorker.js'), {
let lastSemanticJob: LinterTask | undefined;

async function rawLintDocument(
change: TextDocumentChangeEvent<TextDocument>,
document: TextDocument,
sendDiagnostics: (diagnostics: Diagnostic[]) => void,
neo4j: Neo4jSchemaPoller,
) {
const { document } = change;

const query = document.getText();
if (query.length === 0) {
sendDiagnostics([]);
Expand Down
35 changes: 21 additions & 14 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,35 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
import { syntaxColouringLegend } from '@neo4j-cypher/language-support';
import { Neo4jSchemaPoller } from '@neo4j-cypher/schema-poller';
import { doAutoCompletion } from './autocompletion';
import { cleanupWorkers, lintDocument } from './linting';
import { doSignatureHelp } from './signatureHelp';
import { applySyntaxColouringForDocument } from './syntaxColouring';
import { Neo4jSettings } from './types';

const connection = createConnection(ProposedFeatures.all);

import { cleanupWorkers, lintDocument } from './linting';

// Create a simple text document manager.
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);

const neo4jSchemaPoller = new Neo4jSchemaPoller();

async function lintSingleDocument(document: TextDocument): Promise<void> {
return lintDocument(
document,
(diagnostics: Diagnostic[]) => {
void connection.sendDiagnostics({
uri: document.uri,
diagnostics,
});
},
neo4jSchemaPoller,
);
}

function relintAllDocuments() {
void documents.all().map(lintSingleDocument);
}

connection.onInitialize(() => {
const result: InitializeResult = {
capabilities: {
Expand Down Expand Up @@ -73,18 +89,7 @@ connection.onInitialized(() => {
);
});

documents.onDidChangeContent((change) =>
lintDocument(
change,
(diagnostics: Diagnostic[]) => {
void connection.sendDiagnostics({
uri: change.document.uri,
diagnostics,
});
},
neo4jSchemaPoller,
),
);
documents.onDidChangeContent((change) => lintSingleDocument(change.document));

// Trigger the syntax colouring
connection.languages.semanticTokens.on(
Expand All @@ -100,11 +105,13 @@ connection.onNotification(
'connectionUpdated',
(connectionSettings: Neo4jSettings) => {
changeConnection(connectionSettings);
neo4jSchemaPoller.events.once('schemaFetched', relintAllDocuments);
},
);

connection.onNotification('connectionDisconnected', () => {
disconnect();
relintAllDocuments();
});

documents.listen(connection);
Expand Down

0 comments on commit 818fac7

Please sign in to comment.