Skip to content

Commit

Permalink
Change documentSelector to target all cypher, not only files (#191)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Nacho Cordón <[email protected]>
  • Loading branch information
angrykoala and ncordon authored Apr 4, 2024
1 parent d42cccb commit 1e37535
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ connection.onInitialize(() => {
triggerCharacters: ['.', ':', '{', '$', ')'],
},
semanticTokensProvider: {
documentSelector: null,
documentSelector: [{ language: 'cypher' }],
legend: syntaxColouringLegend,
range: false,
full: {
Expand All @@ -60,7 +60,7 @@ connection.onInitialized(() => {
});

const registrationOptions: SemanticTokensRegistrationOptions = {
documentSelector: null,
documentSelector: [{ language: 'cypher' }],
legend: syntaxColouringLegend,
range: false,
full: {
Expand Down
12 changes: 11 additions & 1 deletion packages/vscode-extension/e2e_tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export async function openDocument(docUri: Uri) {
}
}

export async function newUntitledFileWithContent(content: string) {
try {
// The language server will not be activated automatically
const document = await workspace.openTextDocument({ content: content });
await window.showTextDocument(document);
} catch (e) {
console.error(e);
}
}

async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand All @@ -22,7 +32,7 @@ export function getDocumentUri(docName: string) {

export async function eventually(
assertion: () => Promise<void>,
timeoutMs = 10000,
timeoutMs = 1000000,
backoffMs = 100,
) {
let totalWait = 0;
Expand Down
68 changes: 58 additions & 10 deletions packages/vscode-extension/e2e_tests/tests/syntax-validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as assert from 'assert';
import * as vscode from 'vscode';
import { eventually, getDocumentUri, openDocument } from '../helpers';
import {
eventually,
getDocumentUri,
newUntitledFileWithContent,
openDocument,
} from '../helpers';

type InclusionTestArgs = {
textFile: string;
textFile: string | undefined;
expected: vscode.Diagnostic[];
};

Expand All @@ -14,20 +19,39 @@ export async function testSyntaxValidation({
await eventually(
() =>
new Promise((resolve, reject) => {
const docUri = getDocumentUri(textFile);
const diagnostics: vscode.Diagnostic[] =
vscode.languages.getDiagnostics(docUri);

let diagnostics: vscode.Diagnostic[];
if (textFile) {
const docUri = getDocumentUri(textFile);
diagnostics = vscode.languages.getDiagnostics(docUri);
} else {
diagnostics = vscode.languages.getDiagnostics().flatMap((d) => d[1]);
}
try {
// We need to test diagnostics one by one
// because the ones returned by VSCode contain
// more information we don't care about in the tests
assert.equal(diagnostics.length, expected.length);
assert.equal(
diagnostics.length,
expected.length,
'Different length for the diagnostics',
);
diagnostics.forEach((diagnostic, i) => {
const expectedDiagnostic = expected[i];
assert.equal(diagnostic.message, expectedDiagnostic.message);
assert.deepEqual(diagnostic.range, expectedDiagnostic.range);
assert.equal(diagnostic.severity, expectedDiagnostic.severity);
assert.equal(
diagnostic.message,
expectedDiagnostic.message,
'Different message',
);
assert.deepEqual(
diagnostic.range,
expectedDiagnostic.range,
`Different Range`,
);
assert.equal(
diagnostic.severity,
expectedDiagnostic.severity,
'Different Severity',
);
});
resolve();
} catch (e) {
Expand Down Expand Up @@ -89,4 +113,28 @@ suite('Syntax validation spec', () => {
expected: [],
});
});

test.only('Correctly validates a non cypher file when selecting cypher language mode', async () => {
// We open a file that is not saved on disk
// and change the language manually to Cypher
await newUntitledFileWithContent('MATCH (m)');
const editor = vscode.window.activeTextEditor;
await vscode.languages.setTextDocumentLanguage(editor.document, 'cypher');

// We need to wait here because diagnostics are eventually
// consistent i.e. they don't show up immediately
await testSyntaxValidation({
textFile: undefined,
expected: [
new vscode.Diagnostic(
new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(0, 9),
),
'Query cannot conclude with MATCH (must be a RETURN clause, an update clause, a unit subquery call, or a procedure call with no YIELD)',
vscode.DiagnosticSeverity.Error,
),
],
});
});
});
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function activate(context: ExtensionContext) {
// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for Cypher text documents
documentSelector: [{ scheme: 'file', language: 'cypher' }],
documentSelector: [{ language: 'cypher' }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc'),
Expand Down

0 comments on commit 1e37535

Please sign in to comment.