Skip to content

Commit

Permalink
Merge pull request #30 from wader/import-search
Browse files Browse the repository at this point in the history
Add basic import search path support
  • Loading branch information
wader authored Nov 7, 2024
2 parents f75369f + 2a680b0 commit 9276342
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
60 changes: 47 additions & 13 deletions lsp/lsp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,36 @@ def query_walk($uri; $start_env; f):
# TODO: import failure
# TODO: transitive include, max depth
( . #debug({import: .})
| (.include_path.str // .import_path.str) as $path
| [ (.include_path.str // .import_path.str) as $path
| if .meta.keyvals then
# [key: {str: "a"}, val: {str: "b"}] => {a: [b]}
# [key: {str: "a"}, val: [{array: {elems: [{str: "b"}]}}] => {a: [b]}
( .meta.keyvals
| map(
( .key = .key.str
| .value =
( .val
| if .str then [.str.str]
else
( .array.elems
| map(.str.str)
)
end
)
)
)
# {search: ["a","b"]} => "a/path", "b/path
| from_entries
| .search[]?
| "\(.)/\($path)"
)
else $path
end
] as $paths
| .import_alias as $import_alias
| (($uri | uri_resolve($path)) + ".jq") as $include_uri
| [ $paths[] as $path
| ($uri | uri_resolve($path)) + ".jq"
] as $include_uris
| if $import_alias | (. == null | not) and (.str | startswith("$")) then
# import "f" as $name
( $import_alias
Expand All @@ -314,17 +341,24 @@ def query_walk($uri; $start_env; f):
}
)
else
# include "f"
# import "f" as name
try
( $include_uri
| file_uri_to_local
| readfile
| query_fromstring
| .func_defs[]?
| _func_def_env($include_uri; $import_alias)
)
catch empty
( first(
# include "f"
# include "f" {search: "path"}
# import "f" as name
# import "f" as name {search: "path"}
( $include_uris[]
| try
( . as $include_uri
| file_uri_to_local
| readfile
| [query_fromstring, $include_uri]
)
catch empty
)
) as [$query, $uri]
| $query.func_defs[]?
| _func_def_env($uri; $import_alias)
)
end
);

Expand Down
1 change: 1 addition & 0 deletions lsp/testdata/import_search/a/b.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def c: 123;
13 changes: 13 additions & 0 deletions lsp/testdata/import_search/import.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
include "b" {search: "a"};
import "b" as ns1 {search: "a"};
import "b" as ns2 {search: ["a"]};
import "b" as ns3 {search: ["missing","a"]};
import "b" as $name1 {search: "a"};
import "b" as $name2 {search: ["a"]};

c,
ns1::c,
ns2::c,
ns3::c,
$name1,
$name2
49 changes: 49 additions & 0 deletions lsp/testdata/import_search/import.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{
"request": {
"id": 0,
"jsonrpc": "2.0",
"method": "initialize"
},
"response": {
"id": 0,
"result": {
"capabilities": {
"completionProvider": {},
"definitionProvider": true,
"documentSymbolProvider": true,
"hoverProvider": true,
"publishDiagnostics": {},
"textDocumentSync": 1,
"workspace": {
"workspaceFolders": {
"supported": true
}
}
}
}
}
},
{
"request": {
"id": 1,
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"languageId": "plaintext",
"text": "@import.jq",
"uri": "file:///import.jq",
"version": 1
}
}
},
"response": {
"method": "textDocument/publishDiagnostics",
"params": {
"diagnostics": [],
"uri": "file:///import.jq"
}
}
}
]

0 comments on commit 9276342

Please sign in to comment.