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

Adds formatting capabilities to vs-code extension #402

Merged
merged 13 commits into from
Mar 12, 2025
34 changes: 34 additions & 0 deletions packages/language-server/src/formatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { formatQuery } from '@neo4j-cypher/language-support';
import { TextDocument } from 'vscode-languageserver-textdocument';
import {
DocumentFormattingParams,
TextDocuments,
TextEdit,
} from 'vscode-languageserver/node';

export const formatDocument = (
params: DocumentFormattingParams,
documents: TextDocuments<TextDocument>,
): TextEdit[] => {
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}

const text = document.getText();
const formattedText = formatQuery(text);

if (text === formattedText) {
return [];
}

return [
TextEdit.replace(
{
start: { line: 0, character: 0 },
end: { line: document.lineCount, character: 0 },
},
formattedText,
),
];
};
6 changes: 6 additions & 0 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@neo4j-cypher/language-support';
import { Neo4jSchemaPoller } from '@neo4j-cypher/schema-poller';
import { doAutoCompletion } from './autocompletion';
import { formatDocument } from './formatting';
import { cleanupWorkers, lintDocument } from './linting';
import { doSignatureHelp } from './signatureHelp';
import { applySyntaxColouringForDocument } from './syntaxColouring';
Expand Down Expand Up @@ -79,6 +80,7 @@ connection.onInitialize(() => {
signatureHelpProvider: {
triggerCharacters: ['(', ',', ')'],
},
documentFormattingProvider: true,
},
};

Expand Down Expand Up @@ -130,6 +132,10 @@ connection.onNotification(
},
);

connection.onDocumentFormatting((params) => {
return formatDocument(params, documents);
});

connection.onNotification('connectionDisconnected', () => {
disconnect();
relintAllDocuments();
Expand Down
4 changes: 4 additions & 0 deletions packages/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# neo4j-for-vscode

## 1.8.0

- Adds support for formatting a Cypher file according to styleguide rules

## 1.7.0

- Adds support for multiple Neo4j connections
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Our extension preview provides a rich set of features for working with Cypher, t
- Syntax checking - both simple and semantic errors (e.g. type errors, unknown labels, etc)
- Autocompletion for Cypher keywords, functions, labels, properties, database names and more
- Signature help for functions - shows the signature of the function while typing
- Formatting - format the document according to the Cypher styleguide
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


![demo-gif](https://github.com/neo4j/cypher-language-support/blob/main/packages/vscode-extension/resources/images/demo.gif?raw=true)

Expand Down Expand Up @@ -82,7 +83,6 @@ We're working on adding more features to the extension, such as:
- Improved database connection management
- Embedded cypher support in other file types
- Query execution and result visualization
- Automatic query formatting

## Extension settings

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.7.0",
"version": "1.8.0",
"preview": true,
"categories": [
"Programming Languages",
Expand Down
17 changes: 17 additions & 0 deletions packages/vscode-extension/tests/specs/api/formatting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as assert from 'assert';
import * as vscode from 'vscode';
import { newUntitledFileWithContent } from '../../helpers';

suite('Formatting', () => {
test('tests that formatting document works', async () => {
const query = `match (p: Person) where p.name = "John Doe" reTUrn p lIMIt 25`;
const document = await newUntitledFileWithContent(query);
await vscode.commands.executeCommand('editor.action.formatDocument');
const formattedText = document.getText();
const expected = `MATCH (p:Person)
WHERE p.name = "John Doe"
RETURN p
LIMIT 25`;
assert.equal(formattedText, expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function testSyntaxValidation({
);
}

suite.only('Syntax validation spec', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

suite('Syntax validation spec', () => {
afterEach(async () => {
await toggleLinting(true);
});
Expand Down