Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
feat: add semantic folding feature to vscode (#59)
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
OmarTawfik authored Mar 12, 2019
1 parent c0c674e commit 9ae8037
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 46 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"postinstall": "node ./node_modules/vscode/bin/install"
},
"engines": {
"vscode": "^1.23.0"
"vscode": "1.31.1"
},
"commitlint": {
"extends": [
Expand Down Expand Up @@ -93,7 +93,7 @@
"dependencies": {
"@octokit/webhooks-definitions": "1.1.2",
"chalk": "2.4.2",
"vscode-languageclient": "3.3.0",
"vscode-languageserver": "3.3.0"
"vscode-languageclient": "5.2.1",
"vscode-languageserver": "5.2.1"
}
}
13 changes: 12 additions & 1 deletion src/binding/bound-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ import {
ObjectMemberSyntax,
BasePropertySyntax,
} from "../parsing/syntax-nodes";
import { filterUndefined } from "../util/array-utils";
import { Token } from "../scanning/tokens";

function filterUndefined<T>(...items: (T | undefined)[]): T[] {
const result = Array<T>();

items.forEach(item => {
if (item) {
result.push(item);
}
});

return result;
}

export enum BoundKind {
// Top level
Document,
Expand Down
4 changes: 3 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Disposable,
} from "vscode-languageserver";
import { DiagnosticsService } from "./services/diagnostics";
import { FoldingService } from "./services/folding";

export interface LanguageService extends Disposable {
activate(connection: IConnection, documents: TextDocuments): void;
Expand All @@ -19,7 +20,7 @@ export interface LanguageService extends Disposable {
const connection = createConnection(new IPCMessageReader(process), new IPCMessageWriter(process));
const documents: TextDocuments = new TextDocuments();

const services: ReadonlyArray<LanguageService> = [new DiagnosticsService()];
const services: ReadonlyArray<LanguageService> = [new DiagnosticsService(), new FoldingService()];

services.forEach(service => {
service.activate(connection, documents);
Expand All @@ -29,6 +30,7 @@ connection.onInitialize(() => {
return {
capabilities: {
textDocumentSync: documents.syncKind,
foldingRangeProvider: true,
},
};
});
Expand Down
11 changes: 5 additions & 6 deletions src/services/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import { LanguageService } from "../server";
export class DiagnosticsService implements LanguageService {
public activate(connection: IConnection, documents: TextDocuments): void {
connection.onDidOpenTextDocument(params => {
const { uri, text } = params.textDocument;
connection.sendDiagnostics({
uri,
diagnostics: this.provideDiagnostics(text),
uri: params.textDocument.uri,
diagnostics: this.provideDiagnostics(params.textDocument.text),
});
});

documents.onDidChangeContent(({ document }) => {
documents.onDidChangeContent(params => {
connection.sendDiagnostics({
uri: document.uri,
diagnostics: this.provideDiagnostics(document.getText()),
uri: params.document.uri,
diagnostics: this.provideDiagnostics(params.document.getText()),
});
});

Expand Down
27 changes: 27 additions & 0 deletions src/services/folding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* Copyright 2019 Omar Tawfik. Please see LICENSE file at the root of this repository.
*/

import { IConnection, TextDocuments, FoldingRangeKind, FoldingRange } from "vscode-languageserver";
import { Compilation } from "../util/compilation";
import { LanguageService } from "../server";

export class FoldingService implements LanguageService {
public activate(connection: IConnection, documents: TextDocuments): void {
connection.onFoldingRanges(params => {
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}

const compilation = new Compilation(document.getText());
return compilation.syntax.blocks.map(block => {
const start = block.openBracket.range.start;
const end = block.closeBracket.range.end;
return FoldingRange.create(start.line, end.line, start.column, end.column, FoldingRangeKind.Region);
});
});
}

public dispose(): void {}
}
15 changes: 0 additions & 15 deletions src/util/array-utils.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/util/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import { analyzeProperties } from "../analysis/properties-analyzer";

export class Compilation {
private readonly bag: DiagnosticBag;
private readonly tokens: ReadonlyArray<Token>;
private readonly syntax: DocumentSyntax;
private readonly document: BoundDocument;

public readonly tokens: ReadonlyArray<Token>;
public readonly syntax: DocumentSyntax;
public readonly document: BoundDocument;

public constructor(public readonly text: string) {
this.bag = new DiagnosticBag();
Expand Down
47 changes: 30 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8649,31 +8649,44 @@ vsce@1.58.0:
yauzl "^2.3.1"
yazl "^2.2.2"

vscode-jsonrpc@^3.3.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/vscode-jsonrpc/-/vscode-jsonrpc-3.6.2.tgz#3b5eef691159a15556ecc500e9a8a0dd143470c8"
integrity sha512-T24Jb5V48e4VgYliUXMnZ379ItbrXgOimweKaJshD84z+8q7ZOZjJan0MeDe+Ugb+uqERDVV8SBmemaGMSMugA==
vscode-jsonrpc@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/vscode-jsonrpc/-/vscode-jsonrpc-4.0.0.tgz#a7bf74ef3254d0a0c272fab15c82128e378b3be9"
integrity sha512-perEnXQdQOJMTDFNv+UF3h1Y0z4iSiaN9jIlb0OqIYgosPCZGYh/MCUlkFtV2668PL69lRDO32hmvL2yiidUYg==

vscode-languageclient@3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/vscode-languageclient/-/vscode-languageclient-3.3.0.tgz#c761d020f9689acc8a8a5bae51453f381903493c"
integrity sha512-4HVt0GorAV7lJfoT2C6qh/Fug9u/HSmKUa8u+y+Pte0HqvUtOwTI8qlX1vu8vyQ5OHD8t8pJad/9yIbYntwxCw==
vscode-languageclient@5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/vscode-languageclient/-/vscode-languageclient-5.2.1.tgz#7cfc83a294c409f58cfa2b910a8cfeaad0397193"
integrity sha512-7jrS/9WnV0ruqPamN1nE7qCxn0phkH5LjSgSp9h6qoJGoeAKzwKz/PF6M+iGA/aklx4GLZg1prddhEPQtuXI1Q==
dependencies:
vscode-jsonrpc "^3.3.0"
vscode-languageserver-types "^3.3.0"
semver "^5.5.0"
vscode-languageserver-protocol "3.14.1"

vscode-languageserver-types@^3.3.0:
vscode-languageserver-protocol@3.14.1:
version "3.14.1"
resolved "https://registry.yarnpkg.com/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.14.1.tgz#b8aab6afae2849c84a8983d39a1cf742417afe2f"
integrity sha512-IL66BLb2g20uIKog5Y2dQ0IiigW0XKrvmWiOvc0yXw80z3tMEzEnHjaGAb3ENuU7MnQqgnYJ1Cl2l9RvNgDi4g==
dependencies:
vscode-jsonrpc "^4.0.0"
vscode-languageserver-types "3.14.0"

vscode-languageserver-types@3.14.0:
version "3.14.0"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.14.0.tgz#d3b5952246d30e5241592b6dde8280e03942e743"
integrity sha512-lTmS6AlAlMHOvPQemVwo3CezxBp0sNB95KNPkqp3Nxd5VFEnuG1ByM0zlRWos0zjO3ZWtkvhal0COgiV1xIA4A==

vscode-languageserver@3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/vscode-languageserver/-/vscode-languageserver-3.3.0.tgz#f547d4f0e5702f88ff3695bae5905f9604c8cc62"
integrity sha512-8YvEbjl77Nl24/cgcSbQC/CPEhSoJwvLLKGJZJbIF4DsGo4jrVDbuARXfmUt9S6vCEEr++o3fbbZ17iLZ0QH0A==
vscode-languageserver@5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/vscode-languageserver/-/vscode-languageserver-5.2.1.tgz#0d2feddd33f92aadf5da32450df498d52f6f14eb"
integrity sha512-GuayqdKZqAwwaCUjDvMTAVRPJOp/SLON3mJ07eGsx/Iq9HjRymhKWztX41rISqDKhHVVyFM+IywICyZDla6U3A==
dependencies:
vscode-jsonrpc "^3.3.0"
vscode-languageserver-types "^3.3.0"
vscode-languageserver-protocol "3.14.1"
vscode-uri "^1.0.6"

vscode-uri@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-1.0.6.tgz#6b8f141b0bbc44ad7b07e94f82f168ac7608ad4d"
integrity sha512-sLI2L0uGov3wKVb9EB+vIQBl9tVP90nqRvxSoJ35vI3NjxE8jfsE5DSOhWgSunHSZmKS4OCi2jrtfxK7uyp2ww==

vscode@1.1.30:
version "1.1.30"
Expand Down

0 comments on commit 9ae8037

Please sign in to comment.