Skip to content

Commit

Permalink
Merge pull request #26 from wader/import-as-binding
Browse files Browse the repository at this point in the history
Add import "..." as $name support
  • Loading branch information
wader authored Nov 6, 2024
2 parents 8cbbf6d + 9761ae1 commit 10b38ac
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
2 changes: 0 additions & 2 deletions lsp/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"

"github.com/itchyny/gojq"
"github.com/wader/jq-lsp/gojqparser"
Expand Down Expand Up @@ -57,7 +56,6 @@ func (ee parseError) Error() string {
}

func queryErrorPosition(v error) int {
log.Printf("v: %#+v\n", v)
if pe, ok := v.(*gojqparser.ParseError); ok { //nolint:errorlint
return pe.Offset
}
Expand Down
37 changes: 27 additions & 10 deletions lsp/lsp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,33 @@ def query_walk($uri; $start_env; f):
( . #debug({import: .})
| (.include_path.str // .import_path.str) as $path
| .import_alias as $import_alias
| ($uri | uri_resolve($path)) + ".jq"
| . as $include_uri
| file_uri_to_local
| try
( readfile
| query_fromstring
| .func_defs[]?
| _func_def_env($include_uri; $import_alias)
| (($uri | uri_resolve($path)) + ".jq") as $include_uri
| if $import_alias | (. == null | not) and (.str | startswith("$")) then
# import "f" as $name
( $import_alias
| { (.str):
{ str: .str
, start: .start
, stop: .stop
, type: "binding"
, uri: $uri
, args: []
}
}
)
catch empty
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
end
);

def _term_traverse($env):
Expand Down Expand Up @@ -413,7 +430,7 @@ def query_walk($uri; $start_env; f):
end
);

( # inject #include ".jq.lsp" to allow adding additional builtins
( # inject #include ".jq-lsp" to allow adding additional builtins
( [{include_path: {str: ".jq-lsp"}}]
| map(_import_env)
) as $dotjqlsp_envs
Expand Down
4 changes: 4 additions & 0 deletions lsp/testdata/import_as_binding.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "test" as $name;
import "test" as $name2;
$name |
$name2
49 changes: 49 additions & 0 deletions lsp/testdata/import_as_binding.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_as_binding.jq",
"uri": "file:///import_as_binding.jq",
"version": 1
}
}
},
"response": {
"method": "textDocument/publishDiagnostics",
"params": {
"diagnostics": [],
"uri": "file:///import_as_binding.jq"
}
}
}
]
5 changes: 4 additions & 1 deletion lsp/testdata/include_valid.jq
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
include "valid";
include "valid";

fn |
fn2

0 comments on commit 10b38ac

Please sign in to comment.