OpenModelica / modelica-language-server

A VS Code language server extension for Modelica.
Other
5 stars 2 forks source link

Incremental parsing #21

Closed AnHeuermann closed 3 months ago

AnHeuermann commented 5 months ago

Tree-sitter can use incremental parsing to speedup the re-parsing of files, see https://tree-sitter.github.io/tree-sitter/using-parsers#editing.

Currently modelica-language-server always parses the whole file on every change: https://github.com/OpenModelica/modelica-language-server/blob/a52ced7a3cdb3d050106f5edb9323e7c4797199a/server/src/analyzer.ts#L73

To incrementally parse a file the language server needs to track changes from the client. So all changed locations need to be added to the tree:

// first parsing
const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);

// Replace 'let' with 'const'
const newSourceCode = 'const x = 1; console.log(x);';

tree.edit({
  startIndex: 0,
  oldEndIndex: 3,
  newEndIndex: 5,
  startPosition: {row: 0, column: 0},
  oldEndPosition: {row: 0, column: 3},
  newEndPosition: {row: 0, column: 5},
});

const newTree = parser.parse(newSourceCode, tree);

(from https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md#editing)