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

Prototyping a runtime strategy to allow different runtime behaviours in the language server #247

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions packages/language-server/src/autocompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {

import type { CompletionItem } from '@neo4j-cypher/language-support';
import { autocomplete } from '@neo4j-cypher/language-support';
import { Neo4jSchemaPoller } from '@neo4j-cypher/schema-poller';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { RuntimeStrategy } from './runtime/runtimeStrategy';

export function doAutoCompletion(
documents: TextDocuments<TextDocument>,
neo4j: Neo4jSchemaPoller,
runtime: RuntimeStrategy,
) {
return (completionParams: CompletionParams) => {
const dbSchema = runtime.getDbSchema();
const textDocument = documents.get(completionParams.textDocument.uri);
if (textDocument === undefined) return [];

Expand All @@ -23,7 +24,7 @@ export function doAutoCompletion(

const completions: CompletionItem[] = autocomplete(
textDocument.getText(),
neo4j.metadata?.dbSchema ?? {},
dbSchema,
offset,
completionParams.context.triggerKind === CompletionTriggerKind.Invoked,
);
Expand Down
6 changes: 3 additions & 3 deletions packages/language-server/src/linting.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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 { TextDocument } from 'vscode-languageserver-textdocument';
import workerpool from 'workerpool';
import { LinterTask, LintWorker } from './lintWorker';
import { RuntimeStrategy } from './runtime/runtimeStrategy';

const pool = workerpool.pool(join(__dirname, 'lintWorker.js'), {
minWorkers: 2,
Expand All @@ -17,8 +17,9 @@ let lastSemanticJob: LinterTask | undefined;
async function rawLintDocument(
change: TextDocumentChangeEvent<TextDocument>,
sendDiagnostics: (diagnostics: Diagnostic[]) => void,
neo4j: Neo4jSchemaPoller,
runtime: RuntimeStrategy,
) {
const dbSchema = runtime.getDbSchema();
const { document } = change;

const query = document.getText();
Expand All @@ -27,7 +28,6 @@ async function rawLintDocument(
return;
}

const dbSchema = neo4j.metadata?.dbSchema ?? {};
const syntaxErrors = validateSyntax(query, dbSchema);

sendDiagnostics(syntaxErrors);
Expand Down
17 changes: 17 additions & 0 deletions packages/language-server/src/runtime/extensionRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DbSchema } from '@neo4j-cypher/language-support';
import { _Connection } from 'vscode-languageserver';
import { RuntimeStrategy } from './runtimeStrategy';

export class ExtensionRuntime implements RuntimeStrategy {
private _dbSchema: DbSchema | undefined;

constructor(connection: _Connection) {
connection.onNotification('schemaFetched', (dbSchema: DbSchema) => {
this._dbSchema = dbSchema;
});
}

getDbSchema(): DbSchema {
return this._dbSchema ?? {};
}
}
16 changes: 16 additions & 0 deletions packages/language-server/src/runtime/runtimeFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { _Connection } from 'vscode-languageserver';
import { ExtensionRuntime } from './extensionRuntime';
import { RuntimeStrategy } from './runtimeStrategy';
import { StandaloneRuntime } from './standaloneRuntime';

export function getRuntime(
clientName: string,
connection: _Connection,
): RuntimeStrategy {
switch (clientName) {
case 'Visual Studio Code':
return new ExtensionRuntime(connection);
default:
return new StandaloneRuntime(connection);
}
}
5 changes: 5 additions & 0 deletions packages/language-server/src/runtime/runtimeStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DbSchema } from '@neo4j-cypher/language-support';

export interface RuntimeStrategy {
getDbSchema(): DbSchema;
}
49 changes: 49 additions & 0 deletions packages/language-server/src/runtime/standaloneRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { DbSchema } from '@neo4j-cypher/language-support';
import { Neo4jSchemaPoller } from '@neo4j-cypher/schema-poller';
import { _Connection } from 'vscode-languageserver';
import { Neo4jSettings } from '../types';
import { RuntimeStrategy } from './runtimeStrategy';

export class StandaloneRuntime implements RuntimeStrategy {
private _schemaPoller: Neo4jSchemaPoller;

constructor(connection: _Connection) {
this._schemaPoller = new Neo4jSchemaPoller();

connection.onDidChangeConfiguration(
(params: { settings: { neo4j: Neo4jSettings } }) => {
this.changeConnection(params.settings.neo4j);
},
);
}

getDbSchema(): DbSchema {
return this._schemaPoller.metadata?.dbSchema ?? {};
}

changeConnection(connectionSettings: Neo4jSettings): void {
this.disconnect();

if (
this._schemaPoller.connection === undefined &&
connectionSettings.connect &&
connectionSettings.password &&
connectionSettings.connectURL &&
connectionSettings.user
) {
void this._schemaPoller.persistentConnect(
connectionSettings.connectURL,
{
username: connectionSettings.user,
password: connectionSettings.password,
},
{ appName: 'cypher-language-server' },
connectionSettings.database,
);
}
}

disconnect(): void {
this._schemaPoller.disconnect();
}
}
98 changes: 33 additions & 65 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
createConnection,
Diagnostic,
DidChangeConfigurationNotification,
InitializeParams,
InitializeResult,
ProposedFeatures,
SemanticTokensRegistrationOptions,
Expand All @@ -13,22 +14,24 @@ import {
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 { doSignatureHelp } from './signatureHelp';
import { applySyntaxColouringForDocument } from './syntaxColouring';
import { Neo4jSettings } from './types';

const connection = createConnection(ProposedFeatures.all);

import { cleanupWorkers, lintDocument } from './linting';
import { getRuntime } from './runtime/runtimeFactory';
import { RuntimeStrategy } from './runtime/runtimeStrategy';

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

const neo4jSchemaPoller = new Neo4jSchemaPoller();
let runtimeStrategy: RuntimeStrategy;

connection.onInitialize((params: InitializeParams): InitializeResult => {
runtimeStrategy = getRuntime(params.clientInfo?.name ?? '', connection);

connection.onInitialize(() => {
const result: InitializeResult = {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Full,
Expand Down Expand Up @@ -71,72 +74,37 @@ connection.onInitialized(() => {
SemanticTokensRegistrationType.type,
registrationOptions,
);
});

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

// Trigger the syntax colouring
connection.languages.semanticTokens.on(
applySyntaxColouringForDocument(documents),
);

// Trigger the signature help, providing info about functions / procedures
connection.onSignatureHelp(doSignatureHelp(documents, neo4jSchemaPoller));
// Trigger the auto completion
connection.onCompletion(doAutoCompletion(documents, neo4jSchemaPoller));

connection.onNotification(
'connectionUpdated',
(connectionSettings: Neo4jSettings) => {
changeConnection(connectionSettings);
},
);

connection.onNotification('connectionDisconnected', () => {
disconnect();
});
// Trigger linting
documents.onDidChangeContent((change) =>
lintDocument(
change,
(diagnostics: Diagnostic[]) => {
void connection.sendDiagnostics({
uri: change.document.uri,
diagnostics,
});
},
runtimeStrategy,
),
);

// Trigger the signature help, providing info about functions / procedures
connection.onSignatureHelp(doSignatureHelp(documents, runtimeStrategy));

documents.listen(connection);
// Trigger the auto completion
connection.onCompletion(doAutoCompletion(documents, runtimeStrategy));

// Trigger the syntax colouring
connection.languages.semanticTokens.on(
applySyntaxColouringForDocument(documents),
);

documents.listen(connection);
});

connection.listen();

connection.onExit(() => {
cleanupWorkers();
});

const changeConnection = (connectionSettings: Neo4jSettings) => {
disconnect();

if (
neo4jSchemaPoller.connection === undefined &&
connectionSettings.connect &&
connectionSettings.password &&
connectionSettings.connectURL &&
connectionSettings.user
) {
void neo4jSchemaPoller.persistentConnect(
connectionSettings.connectURL,
{
username: connectionSettings.user,
password: connectionSettings.password,
},
{ appName: 'cypher-language-server' },
connectionSettings.database,
);
}
};

const disconnect = () => {
neo4jSchemaPoller.disconnect();
};
11 changes: 4 additions & 7 deletions packages/language-server/src/signatureHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
TextDocuments,
} from 'vscode-languageserver/node';

import { Neo4jSchemaPoller } from '@neo4j-cypher/schema-poller';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { RuntimeStrategy } from './runtime/runtimeStrategy';

export const emptyResult: SignatureHelp = {
signatures: [],
Expand All @@ -16,20 +16,17 @@ export const emptyResult: SignatureHelp = {

export function doSignatureHelp(
documents: TextDocuments<TextDocument>,
neo4j: Neo4jSchemaPoller,
runtime: RuntimeStrategy,
) {
return (params: SignatureHelpParams) => {
const dbSchema = runtime.getDbSchema();
const textDocument = documents.get(params.textDocument.uri);
const endOfTriggerHelp = params.context?.triggerCharacter === ')';
if (textDocument === undefined || endOfTriggerHelp) return emptyResult;

const position = params.position;
const offset = textDocument.offsetAt(position);

return signatureHelp(
textDocument.getText(),
neo4j.metadata?.dbSchema ?? {},
offset,
);
return signatureHelp(textDocument.getText(), dbSchema, offset);
};
}
2 changes: 1 addition & 1 deletion packages/schema-poller/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { FRIENDLY_ERROR_MESSAGES } from './connectionErrorHandler';
export type { ConnectionError } from './connectionErrorHandler';
export type { MetadataPoller } from './metadataPoller';
export { MetadataPoller } from './metadataPoller';
export type { Neo4jConnection } from './neo4jConnection';
export type { Database } from './queries/databases';
export { Neo4jSchemaPoller } from './schemaPoller';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Neo4jSettings } from '@neo4j-cypher/language-server/src/types';
import { DbSchema } from '@neo4j-cypher/language-support/dist/types';

export class MockLanguageClient {
async sendNotification(
methodName: string,
settings?: Neo4jSettings,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
dbSchema?: DbSchema,
): Promise<void> {
await Promise.resolve(
`sending notification using ${methodName} with ${settings?.connectURL}, ${settings?.connect}, ${settings?.database}, ${settings?.password}, ${settings?.trace.server}, ${settings?.user}`,
);
await Promise.resolve(`sending notification using ${methodName}`);
}
}
Loading
Loading