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 7d8b395
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
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

0 comments on commit 7d8b395

Please sign in to comment.