Skip to content

Commit

Permalink
Tags for code navigation (#7)
Browse files Browse the repository at this point in the history
* Removing test.mo
* Adding class definition tags
* Adding variable definitions tags
* Updating readme
  • Loading branch information
AnHeuermann authored Apr 17, 2024
1 parent 93def7a commit 2972fa1
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 13 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ To test the highlighting configure run:
npx tree-sitter highlight examples/Main.mo
```

## Tags

tree-sitter-metamodelica supports
[tagging for code navigation systems](https://tree-sitter.github.io/tree-sitter/code-navigation-systems)
to provide a list of all definitions.

```bash
npx tree-sitter tags examples/Main.mo
```


## Usage

Use [Web Tree-sitter](https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md)
Expand All @@ -75,11 +86,15 @@ parser.setLanguage(MetaModelica)

## Current Status

tree-sitter-metamodelica has been tested on a "Save Total" version of the
[Modelica.Fluid.Examples.DrumBoiler.DrumBoiler](./examples/DrumBoiler.mo) which
was successfully parsed and highlighted.
tree-sitter-metamodelica has been tested on all of the MetaModelica files of the
OpenModelica Compiler and can parse all but the following features:

- Susan / Template interface packages
- `code_equations`


```bash
npx tree-sitter parse examples/DrumBoiler.mo
npx tree-sitter highlight examples/DrumBoiler.mo
npx tree-sitter parse examples/Main.mo
npx tree-sitter highlight examples/Main.mo
npx tree-sitter tags examples/Main.mo
```
4 changes: 0 additions & 4 deletions examples/test.mo

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"file-types": [
"mo"
],
"highlights": "queries/highlights.scm"
"highlights": "queries/highlights.scm",
"tags": "queries/tags.scm"
}
]
}
9 changes: 6 additions & 3 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[
(BLOCK_COMMENT) ;; >/* comment */<
(COMMENT) ;; >// comment<
]
] @comment
(string_comment (STRING) @comment) ;; model foo >"description"<

;;; Numbers
Expand All @@ -36,7 +36,11 @@
(polymorphic_type_specifier (name_path)@type)

;;; Variables
(declaration (IDENT) @variable.parameter) ;; Real >x<
(declaration [
(IDENT)
(OPERATOR)
] @variable.parameter
) ;; Real >x<
(component_reference__function_call componentReference: (component_reference) @variable.parameter) ;; >x<

;;; Function calls
Expand Down Expand Up @@ -107,7 +111,6 @@
(MATCH)
(MATCHCONTINUE)
(MODEL)
(OPERATOR)
(OPTIMIZATION)
(OVERLOAD)
(PACKAGE)
Expand Down
168 changes: 168 additions & 0 deletions queries/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
;;;;; Tagging for code navigation

;;; Class definitions

(class_definition
(class_type
class: (CLASS)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.class

(class_definition
(class_type
optimization: (OPTIMIZATION)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.optimization

(class_definition
(class_type
model: (MODEL)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.model

(class_definition
(class_type
record: (RECORD)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.record

(class_definition
(class_type
connector: (CONNECTOR)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.connector

(class_definition
(class_type
type: (TYPE)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.type

(class_definition
(class_type
package: (PACKAGE)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.package

(class_definition
(class_type
function: (FUNCTION)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.function

(class_definition
(class_type
uniontype: (UNIONTYPE)
)
(class_specifier
identifier: (identifier) @name
comment: (string_comment
(STRING) @doc
(
(PLUS)
(STRING) @doc
)*
)*
)
(#strip! @doc "^\\\"|\\\"$")
) @definition.uniontype

;;; Variable definitions

(component_declaration
declaration: (declaration
[
identifier: (IDENT) @name
(OPERATOR) @name
]
)
comment: (comment)* @doc
) @definition.variable
25 changes: 25 additions & 0 deletions test/highlight/Scopes.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package A "Package"
function foo
end foo;

function bar
algorithm
foo(); // In scope
end bar;
end A;

package B
function bar1
Real x;
algorithm
x := foo(); // Not in scope
x := A.foo(); // In scope
end bar1;

function bar2
Real y;
algorithm
show(x); // Not in scope
show(y); // In scope
end bar2;
end B;
40 changes: 40 additions & 0 deletions test/tags/ClassDefinitions.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class C1 "Documentation"
// ^ definition.class
end C1;

class C2
// ^ definition.class
"Documentation"
end C2;

optimization Opt "This has" + "a lot of documentation"
// ^ definition.optimization
end Opt;

model MyModel
// ^ definition.model
end MyModel;

record R
// ^ definition.record
end R;

connector C
// ^ definition.connector
end C;

type T
// ^ definition.type
end T;

package PackageA
// ^ definition.package
end PackageA;

function foo
// ^ definition.function
end foo;

uniontype T2
// ^ definition.uniontype
end T2;
24 changes: 24 additions & 0 deletions test/tags/MatchLocalDefinitions.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function foo "Some
longish documentation."
input T1 x;
// ^ definition.variable
algorithm
() := match x
local
Real y1;
// ^ definition.variable
list<T2> y2;
// ^ definition.variable
list<T> y3;
// ^ definition.variable
list<T<T, list<T>>> y4;
// ^ definition.variable
case T1.R(_)
algorithm
doStuff(y1, y2);
then ();

else
then fail();
end match;
end foo;
30 changes: 30 additions & 0 deletions test/tags/VariableDefinitions.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function foo
input Absyn.Operator operator;
// ^ definition.variable
input Real i1 "This is some input";
// ^ definition.variable
output Real o1 "This is some output";
// ^ definition.variable
protected
Real x1 "Some protected variable";
// ^ definition.variable
Integer x2;
// ^ definition.variable
Boolean x3;
// ^ definition.variable
String x4;
// ^ definition.variable
MyType x5;
// ^ definition.variable
public
List<Real> x6 "Some public variable";
// ^ definition.variable
List<MyType> x7;
// ^ definition.variable
Option<Real> x8;
// ^ definition.variable
Option<MyType> x9;
// ^ definition.variable
Tuple<T1, T2, T3> x10;
// ^ definition.variable
end foo;

0 comments on commit 2972fa1

Please sign in to comment.