Skip to content

Commit

Permalink
LSP: Fix issues where modules share the same name as an Enum/Record
Browse files Browse the repository at this point in the history
  • Loading branch information
jansul committed Aug 5, 2023
1 parent c3ddc13 commit 53e8033
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
record Header {
title : String
}
----------------------------------------------------------------file record.mint
module Header {
fun default : Header {
{ title: "Mint" }
}
}
----------------------------------------------------------------file module.mint
component Test {
fun render : Html {
let header =
Header.default()

<div>
<{ header.title }>
</div>
}
}
------------------------------------------------------------------file test.mint
{
"id": 0,
"method": "initialize",
"params": {
"capabilities": {
"textDocument": {
"definition": {
"linkSupport": false
}
}
}
}
}
-------------------------------------------------------------------------request
{
"jsonrpc": "2.0",
"id": 1,
"params": {
"textDocument": {
"uri": "file://#{root_path}/test.mint"
},
"position": {
"line": 3,
"character": 6
}
},
"method": "textDocument/definition"
}
-------------------------------------------------------------------------request
{
"jsonrpc": "2.0",
"result": {
"range": {
"start": {
"line": 0,
"character": 7
},
"end": {
"line": 0,
"character": 13
}
},
"uri": "file://#{root_path}/module.mint"
},
"id": 1
}
------------------------------------------------------------------------response
2 changes: 1 addition & 1 deletion spec/language_server/definition/location/type_enum
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ enum Status {
Ok
}
----------------------------------------------------------------file status.mint
module Test {
module Status {
fun success : Status {
Status::Ok
}
Expand Down
2 changes: 1 addition & 1 deletion spec/language_server/definition/location/type_record
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ record Article {
title : String
}
---------------------------------------------------------------file article.mint
module Test {
module Article {
fun test : Article {
{
id: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
record Header {
title : String
}
----------------------------------------------------------------file record.mint
module Header {
fun default : Header {
{ title: "Mint" }
}
}
----------------------------------------------------------------file module.mint
component Test {
fun render : Html {
let header =
Header.default()

<div>
<{ header.title }>
</div>
}
}
------------------------------------------------------------------file test.mint
{
"id": 0,
"method": "initialize",
"params": {
"capabilities": {
"textDocument": {
"definition": {
"linkSupport": true
}
}
}
}
}
-------------------------------------------------------------------------request
{
"jsonrpc": "2.0",
"id": 1,
"params": {
"textDocument": {
"uri": "file://#{root_path}/test.mint"
},
"position": {
"line": 3,
"character": 6
}
},
"method": "textDocument/definition"
}
-------------------------------------------------------------------------request
{
"jsonrpc": "2.0",
"result": {
"originSelectionRange": {
"start": {
"line": 3,
"character": 6
},
"end": {
"line": 3,
"character": 12
}
},
"targetUri": "file://#{root_path}/module.mint",
"targetRange": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 4,
"character": 1
}
},
"targetSelectionRange": {
"start": {
"line": 0,
"character": 7
},
"end": {
"line": 0,
"character": 13
}
}
},
"id": 1
}
------------------------------------------------------------------------response
2 changes: 1 addition & 1 deletion spec/language_server/definition/location_link/type_enum
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ enum Status {
Ok
}
----------------------------------------------------------------file status.mint
module Test {
module Status {
fun success : Status {
Status::Ok
}
Expand Down
2 changes: 1 addition & 1 deletion spec/language_server/definition/location_link/type_record
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ record Article {
title : String
}
---------------------------------------------------------------file article.mint
module Test {
module Article {
fun test : Article {
{
id: 1,
Expand Down
40 changes: 22 additions & 18 deletions src/ls/definition/type_id.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@ module Mint
module LS
class Definition < LSP::RequestMessage
def definition(node : Ast::TypeId, server : Server, workspace : Workspace, stack : Array(Ast::Node))
case stack[1]?
when Ast::ModuleAccess
links = workspace.ast.modules.select(&.name.value.==(node.value))
.reject(&.in?(Core.ast.nodes))
.map do |mod|
location_link server, node, mod.name, mod
end

return links.first if links.size == 1
return links unless links.empty?
end

found =
workspace.ast.enums.select(&.name.value.==(node.value)) +
workspace.ast.records.select(&.name.value.==(node.value)) +
workspace.ast.stores.select(&.name.value.==(node.value)) +
workspace.ast.components.select(&.name.value.==(node.value)) +
workspace.ast.modules.select(&.name.value.==(node.value))
workspace.ast.enums.find(&.name.value.==(node.value)) ||
workspace.ast.records.find(&.name.value.==(node.value)) ||
workspace.ast.stores.find(&.name.value.==(node.value)) ||
find_component(workspace, node.value)

if found.empty? && (next_node = stack[1]?)
if found.nil? && (next_node = stack[1])
return definition(next_node, server, workspace, stack)
end

links = found
.reject(&.in?(Core.ast.nodes))
.map do |mod|
case mod
when Ast::Store, Ast::Enum, Ast::Component, Ast::RecordDefinition, Ast::Module
location_link server, node, mod.name, mod
end
end
.compact
return if Core.ast.nodes.includes?(found)

return links if links.size > 1

links.first?
case found
when Ast::Store, Ast::Enum, Ast::Component, Ast::RecordDefinition
location_link server, node, found.name, found
end
end
end
end
Expand Down

0 comments on commit 53e8033

Please sign in to comment.