Skip to content

Commit

Permalink
Add test for incremental parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed May 29, 2024
1 parent d1b6a0e commit ab55fb2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
8 changes: 4 additions & 4 deletions server/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export default class Analyzer {
* @param uri uri to document to add
* @throws if the document does not belong to a library
*/
public addDocument(uri: LSP.DocumentUri): void {
this.#project.addDocument(uriToPath(uri));
public async addDocument(uri: LSP.DocumentUri): Promise<void> {
await this.#project.addDocument(uriToPath(uri));
}

/**
Expand All @@ -110,8 +110,8 @@ export default class Analyzer {
* @param text the modification
* @param range range to update, or `undefined` to replace the whole file
*/
public updateDocument(uri: LSP.DocumentUri, text: string, range?: LSP.Range): void {
this.#project.updateDocument(url.fileURLToPath(uri), text, range);
public async updateDocument(uri: LSP.DocumentUri, text: string, range?: LSP.Range): Promise<void> {
await this.#project.updateDocument(url.fileURLToPath(uri), text, range);
}

/**
Expand Down
32 changes: 18 additions & 14 deletions server/src/project/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@
*
*/

import { TextDocument } from "vscode-languageserver-textdocument";
import * as LSP from "vscode-languageserver/node";
import Parser from "web-tree-sitter";
import * as fs from "node:fs/promises";
import * as url from "node:url";
import * as TreeSitterUtil from "../util/tree-sitter";

import { logger } from "../util/logger";
import { ModelicaLibrary } from "./library";
import { ModelicaProject } from "./project";
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as LSP from 'vscode-languageserver/node';
import Parser from 'web-tree-sitter';
import * as fs from 'node:fs/promises';
import * as TreeSitterUtil from '../util/tree-sitter';

import { logger } from '../util/logger';
import { ModelicaLibrary } from './library';
import { ModelicaProject } from './project';
import { positionToPoint } from '../util/tree-sitter';
import { pathToUri, uriToPath } from '../util';

Expand All @@ -52,7 +51,12 @@ export class ModelicaDocument implements TextDocument {
readonly #document: TextDocument;
#tree: Parser.Tree;

public constructor(project: ModelicaProject, library: ModelicaLibrary | null, document: TextDocument, tree: Parser.Tree) {
public constructor(
project: ModelicaProject,
library: ModelicaLibrary | null,
document: TextDocument,
tree: Parser.Tree,
) {
this.#project = project;
this.#library = library;
this.#document = document;
Expand Down Expand Up @@ -182,19 +186,19 @@ export class ModelicaDocument implements TextDocument {
public get within(): string[] {
const withinClause = this.#tree.rootNode.children
.find((node) => node.type === 'within_clause')
?.childForFieldName("name");
?.childForFieldName('name');
if (!withinClause) {
return [];
}

// TODO: Use a helper function from TreeSitterUtil
const identifiers: string[] = [];
TreeSitterUtil.forEach(withinClause, (node) => {
if (node.type === "name") {
if (node.type === 'name') {
return true;
}

if (node.type === "IDENT") {
if (node.type === 'IDENT') {
identifiers.push(node.text);
}

Expand Down
21 changes: 21 additions & 0 deletions server/src/project/test/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ describe('ModelicaDocument', () => {
assert.equal(document.getText().trim(), UPDATED_TEST_PACKAGE_CONTENT.trim());
});

it('can update incrementally', () => {
const textDocument = createTextDocument('.', TEST_PACKAGE_CONTENT);
const tree = project.parser.parse(TEST_PACKAGE_CONTENT);
const document = new ModelicaDocument(project, library, textDocument, tree);
document.update(
'1.0.1',
{
start: {
line: 1,
character: 22,
},
end: {
line: 1,
character: 27,
},
}
);

assert.equal(document.getText().trim(), UPDATED_TEST_PACKAGE_CONTENT.trim());
});

it('a file with no `within` clause has the correct package path', () => {
const textDocument = createTextDocument('./package.mo', TEST_PACKAGE_CONTENT);
const tree = project.parser.parse(TEST_PACKAGE_CONTENT);
Expand Down
4 changes: 2 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ModelicaServer {
logger.debug('onDidChangeTextDocument');
for (const change of params.contentChanges) {
const range = 'range' in change ? change.range : undefined;
this.#analyzer.updateDocument(params.textDocument.uri, change.text, range);
await this.#analyzer.updateDocument(params.textDocument.uri, change.text, range);
}
}

Expand All @@ -161,7 +161,7 @@ export class ModelicaServer {
for (const change of params.changes) {
switch (change.type) {
case LSP.FileChangeType.Created:
this.#analyzer.addDocument(change.uri);
await this.#analyzer.addDocument(change.uri);
break;
case LSP.FileChangeType.Changed: {
// TODO: incremental?
Expand Down

0 comments on commit ab55fb2

Please sign in to comment.