Skip to content

Commit

Permalink
LSP: Go to definition (#526)
Browse files Browse the repository at this point in the history
* LSP: Go to definition

Implement goto definition in the language service, for variables and
standalone identifiers (much like with hover, there's still a lot of
places that need to have identifier metadata emitted during
typechecking). As those places are updated, they will work for hover as
well as goto definition, which is a big upside!
This also refactors the `Some(...)` AST transformation to use a bogus
Position, so hover and goto definition work for `Some(...)` identifiers.

* Fixing tests since Option.Some and Option.None transformations now use bogus position
  • Loading branch information
kengorab authored Dec 25, 2024
1 parent 7565eaa commit 860fbfe
Show file tree
Hide file tree
Showing 23 changed files with 155 additions and 77 deletions.
3 changes: 2 additions & 1 deletion projects/compiler/src/parser.abra
Original file line number Diff line number Diff line change
Expand Up @@ -1700,10 +1700,11 @@ export type Parser {
_ => false
}
val invokee = if isSome {
val bogusPosition = Position(line: 0, col: 0)
AstNode(
token: token,
kind: AstNodeKind.Accessor(AccessorAstNode(
root: AstNode(token: Token(position: token.position, kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
root: AstNode(token: Token(position: bogusPosition, kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
path: [(Token(position: token.position, kind: TokenKind.Dot), Label(name: "Some", position: token.position))]
))
)
Expand Down
23 changes: 20 additions & 3 deletions projects/compiler/src/typechecker.abra
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,16 @@ export enum IdentifierKindMeta {
Type(isEnum: Bool, typeParams: String[])
}

export enum IdentifierMetaImport {
Prelude
Module(filePath: String)
}

export type IdentifierMeta {
name: String
kind: IdentifierKindMeta
importedFrom: String? = None
importedFrom: IdentifierMetaImport? = None
definitionPosition: Position? = None
}

export type TypedModule {
Expand Down Expand Up @@ -3058,6 +3064,7 @@ export type Typechecker {
val ident = IdentifierMeta(
name: label.name,
kind: IdentifierKindMeta.Variable(mutable: mutable, typeRepr: ty.repr()),
definitionPosition: Some(label.position),
)

val v = (label.position.col - 1, label.position.col + label.name.length - 1, ident)
Expand Down Expand Up @@ -4023,10 +4030,11 @@ export type Typechecker {
}
IdentifierKind.Discard => return Err(TypeError(position: token.position, kind: TypeErrorKind.UnknownName("_", "variable")))
IdentifierKind.None_ => {
val bogusPosition = Position(line: 0, col: 0)
val replacement = AstNode(
token: token,
kind: AstNodeKind.Accessor(AccessorAstNode(
root: AstNode(token: Token(position: token.position, kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
root: AstNode(token: Token(position: bogusPosition, kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
path: [(Token(position: token.position, kind: TokenKind.Dot), Label(name: "None", position: token.position))]
))
)
Expand Down Expand Up @@ -4067,10 +4075,19 @@ export type Typechecker {
VariableAlias.Enum(_enum) => IdentifierKindMeta.Type(isEnum: true, typeParams: _enum.typeParams)
}

val importedFrom = if varImportMod |mod| {
Some(IdentifierMetaImport.Module(mod.name))
} else if variable.scope == self.project.preludeScope {
Some(IdentifierMetaImport.Prelude)
} else {
None
}

val ident = IdentifierMeta(
name: name,
kind: kind,
importedFrom: varImportMod?.name,
importedFrom: importedFrom,
definitionPosition: Some(variable.label.position),
)

val v = (token.position.col - 1, token.position.col + name.length - 1, ident)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"name": "accessor",
"root": {
"token": {
"position": [1, 13],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
12 changes: 6 additions & 6 deletions projects/compiler/test/typechecker/array/array.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [7, 28],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -666,7 +666,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [8, 19],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1069,7 +1069,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [11, 20],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1368,7 +1368,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [12, 30],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1605,7 +1605,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [14, 28],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -2076,7 +2076,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [18, 13],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
2 changes: 1 addition & 1 deletion projects/compiler/test/typechecker/binary/eq.2.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [2, 6],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
2 changes: 1 addition & 1 deletion projects/compiler/test/typechecker/binary/neq.2.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [2, 6],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [8, 16],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
10 changes: 5 additions & 5 deletions projects/compiler/test/typechecker/funcdecl/funcdecl.5.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [3, 49],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -372,7 +372,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [4, 31],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -586,7 +586,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [5, 32],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -812,7 +812,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [6, 32],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -916,7 +916,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [6, 48],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [4, 15],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [1, 15],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
12 changes: 6 additions & 6 deletions projects/compiler/test/typechecker/if/expr.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [21, 3],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1194,7 +1194,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [32, 3],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1427,7 +1427,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [36, 38],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1605,7 +1605,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [39, 19],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1954,7 +1954,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [45, 3],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -2127,7 +2127,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [49, 16],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [3, 15],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -695,7 +695,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [4, 17],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [3, 17],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [4, 19],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [3, 11],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [3, 31],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
6 changes: 3 additions & 3 deletions projects/compiler/test/typechecker/map/map.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [9, 48],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1203,7 +1203,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [11, 36],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down Expand Up @@ -1252,7 +1252,7 @@
"kind": "accessor",
"head": {
"token": {
"position": [11, 43],
"position": [0, 0],
"kind": {
"name": "Ident",
"value": "Option"
Expand Down
Loading

0 comments on commit 860fbfe

Please sign in to comment.