Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relints documents when connecting / disconnecting to a database #318

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
6 changes: 6 additions & 0 deletions packages/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# neo4j-for-vscode

## 1.6.1

- Makes the editor relint all documents when connecting / disconnecting from a database
- Adds warnings for deprecated procedures / functions
- Improves backticking of completions

## 1.6.0

- Updates grammar to LTS version
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publisher": "neo4j-extensions",
"author": "Neo4j Inc.",
"license": "Apache-2.0",
"version": "1.6.0",
"version": "1.7.0",
"preview": true,
"categories": [
"Programming Languages",
Expand Down
2 changes: 2 additions & 0 deletions packages/vscode-extension/tests/fixtures/deprecated-by.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CALL apoc.create.uuids(5);
RETURN apoc.create.uuid();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
newUntitledFileWithContent,
openDocument,
} from '../../helpers';
import { defaultConnectionKey } from '../../suiteSetup';
import {
connectDefault,
defaultConnectionKey,
disconnectDefault,
} from '../../suiteSetup';

type InclusionTestArgs = {
textFile: string | undefined;
Expand Down Expand Up @@ -71,6 +75,41 @@ export async function testSyntaxValidation({
}

suite('Syntax validation spec', () => {
test('Relints when database connected / disconnected', async () => {
const textFile = 'deprecated-by.cypher';
const docUri = getDocumentUri(textFile);

await openDocument(docUri);

const deprecationErrors = [
new vscode.Diagnostic(
new vscode.Range(new vscode.Position(0, 5), new vscode.Position(0, 22)),
'Procedure apoc.create.uuids is deprecated.',
vscode.DiagnosticSeverity.Warning,
),
new vscode.Diagnostic(
new vscode.Range(new vscode.Position(1, 7), new vscode.Position(1, 23)),
'Function apoc.create.uuid is deprecated.',
vscode.DiagnosticSeverity.Warning,
),
];
// We should be connected by default so the errors will be there initially
await testSyntaxValidation({
textFile,
expected: deprecationErrors,
});
await disconnectDefault();
await testSyntaxValidation({
textFile,
expected: [],
});
await connectDefault();
await testSyntaxValidation({
textFile,
expected: deprecationErrors,
});
});

test('Correctly validates empty cypher statement', async () => {
const textFile = 'syntax-validation.cypher';
const docUri = getDocumentUri(textFile);
Expand Down
12 changes: 12 additions & 0 deletions packages/vscode-extension/tests/suiteSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ export async function saveDefaultConnection(): Promise<void> {
);
}

export async function connectDefault(): Promise<void> {
await vscode.commands.executeCommand(CONSTANTS.COMMANDS.CONNECT_COMMAND, {
key: defaultConnectionKey,
});
}

export async function disconnectDefault(): Promise<void> {
await vscode.commands.executeCommand(CONSTANTS.COMMANDS.DISCONNECT_COMMAND, {
key: defaultConnectionKey,
});
}

export async function createTestDatabase(): Promise<void> {
const { scheme, host, port, user, password } = getNeo4jConfiguration();

Expand Down
Loading