Skip to content

Commit 702e596

Browse files
committed
Fix wrong line number for first column errors
Also add --query to eval a query and @path to read a file in tests
1 parent 4e8429d commit 702e596

13 files changed

+133
-19
lines changed

lsp/lsp.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func queryErrorPosition(v error) int {
6464
}
6565

6666
func Run(env Env) error {
67+
query := ""
68+
6769
if len(env.Args) >= 2 && env.Args[1] == "--version" {
6870
fmt.Fprintf(env.Stdout, "%s\n", env.Version)
6971
return nil
@@ -72,10 +74,13 @@ func Run(env Env) error {
7274
jq-lsp - jq language server
7375
For details see https://github.com/wader/jq-lsp
7476
Usage: %s [OPTIONS]
75-
--help Show help
76-
--version Show version
77+
--help Show help
78+
--version Show version
79+
--query QUERY Eval query
7780
`[1:], env.Args[0])
7881
return nil
82+
} else if len(env.Args) >= 3 && env.Args[1] == "--query" {
83+
query = env.Args[2]
7984
}
8085

8186
i := &interp{
@@ -88,7 +93,12 @@ Usage: %s [OPTIONS]
8893
// https://github.com/itchyny/gojq/issues/86 has been resolved
8994
// TODO: could probably reuse *gojq.Code instance
9095
for {
91-
iter, err := i.Eval("serve", state)
96+
s := "serve"
97+
if query != "" {
98+
s = query
99+
}
100+
101+
iter, err := i.Eval(s, state)
92102
if err != nil {
93103
return err
94104
}
@@ -112,9 +122,19 @@ Usage: %s [OPTIONS]
112122
case [2]interface{}:
113123
fmt.Fprintln(env.Stderr, v[:]...)
114124
default:
115-
state = v
125+
if query != "" {
126+
jd := json.NewEncoder(env.Stdout)
127+
jd.SetIndent("", " ")
128+
jd.Encode(v)
129+
} else {
130+
state = v
131+
}
116132
}
117133
}
134+
135+
if query != "" {
136+
return nil
137+
}
118138
}
119139
}
120140

lsp/lsp.jq

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ def _cond(cond; f):
99
end;
1010

1111
def debug(f):
12-
_cond(
13-
env.DEBUG != null;
14-
( (["DEBUG", (. | f)] | tojson)
12+
( ( (["DEBUG", (. | f)] | tojson)
1513
, "\n"
1614
| stderr
1715
)
16+
, .
1817
);
1918
def debug: debug(.);
2019

@@ -76,7 +75,7 @@ def byte_pos_to_lc($pos):
7675
| . as $lens
7776
| [ {i: 0, p: $pos}
7877
| while(
79-
.p > 0;
78+
.p >= 0;
8079
( .p -= $lens[.i]
8180
| .i += 1
8281
)
@@ -798,13 +797,13 @@ def handle($state):
798797
def serve:
799798
( . as $state
800799
| jsonrpc_read as $request
801-
| debug({$request})
800+
#| debug({$request})
802801
| $request
803802
| try handle($state)
804803
catch
805804
if (type != "object" or .response or .state | not) then error end
806805
| ( .response[]?
807-
| debug({response: .})
806+
#| debug({response: .})
808807
| jsonrpc_write
809808
)
810809
, .state // $state

lsp/lsp_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,35 @@ import (
1717

1818
var update = flag.Bool("update", false, "Update tests")
1919

20+
// replace "@path" with content of file
21+
func replaceTextFile(t *testing.T, v any, readFile func(s string) ([]byte, error)) any {
22+
switch v := v.(type) {
23+
case []any:
24+
a := make([]any, len(v))
25+
for i, ve := range v {
26+
a[i] = replaceTextFile(t, ve, readFile)
27+
}
28+
return a
29+
case map[string]any:
30+
a := make(map[string]any, len(v))
31+
for k, ve := range v {
32+
a[k] = replaceTextFile(t, ve, readFile)
33+
}
34+
return a
35+
case string:
36+
if _, path, found := strings.Cut(v, "@"); found {
37+
if b, err := readFile(path); err == nil {
38+
return string(b)
39+
} else {
40+
t.Fatal(err)
41+
}
42+
}
43+
return v
44+
default:
45+
return v
46+
}
47+
}
48+
2049
func TestLSP(t *testing.T) {
2150
difftest.TestWithOptions(t, difftest.Options{
2251
Path: "testdata",
@@ -53,7 +82,8 @@ func TestLSP(t *testing.T) {
5382

5483
stdinBuf := &bytes.Buffer{}
5584
for _, r := range requests {
56-
reqB, err := json.Marshal(&r.Request)
85+
rr := replaceTextFile(t, r.Request, readFile)
86+
reqB, err := json.Marshal(&rr)
5787
if err != nil {
5888
t.Fatal(err)
5989
}

lsp/testdata/did_change_not_found.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"params": {
3333
"contentChanges": [
3434
{
35-
"text": "def fn: abc;"
35+
"text": "@not_found.jq"
3636
}
3737
],
3838
"textDocument": {

lsp/testdata/did_change_syntax_error.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"params": {
3333
"contentChanges": [
3434
{
35-
"text": "def fn 123;"
35+
"text": "@syntax_error.jq"
3636
}
3737
],
3838
"textDocument": {

lsp/testdata/did_change_valid.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"params": {
3333
"contentChanges": [
3434
{
35-
"text": "def fn: 123;"
35+
"text": "@valid.jq"
3636
}
3737
],
3838
"textDocument": {

lsp/testdata/did_open_not_found.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"params": {
3333
"textDocument": {
3434
"languageId": "plaintext",
35-
"text": "def fn: abc;",
35+
"text": "@not_found.jq",
3636
"uri": "file:///not_found.jq",
3737
"version": 1
3838
}

lsp/testdata/did_open_syntax_error.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"params": {
3333
"textDocument": {
3434
"languageId": "plaintext",
35-
"text": "def fn 123;",
35+
"text": "@syntax_error.jq",
3636
"uri": "file:///syntax_error.jq",
3737
"version": 1
3838
}

lsp/testdata/did_open_valid.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"params": {
3333
"textDocument": {
3434
"languageId": "plaintext",
35-
"text": "def fn: 123;",
35+
"text": "@valid.jq",
3636
"uri": "file:///valid.jq",
3737
"version": 1
3838
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
aaa

0 commit comments

Comments
 (0)