-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
249 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package langserver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/kitagry/bqls/langserver/internal/lsp" | ||
"github.com/sourcegraph/jsonrpc2" | ||
) | ||
|
||
type VirtualTextDocumentParams struct { | ||
TextDocument lsp.TextDocumentIdentifier `json:"textDocument"` | ||
} | ||
|
||
type VirtualTextDocument struct { | ||
Contents []lsp.MarkedString `json:"contents"` | ||
} | ||
|
||
type VirtualTextDocumentInfo struct { | ||
ProjectID string | ||
DatasetID string | ||
TableID string | ||
JobID string | ||
} | ||
|
||
func ParseVirtualTextDocument(textDocument lsp.DocumentURI) (VirtualTextDocumentInfo, error) { | ||
suffix, ok := strings.CutPrefix(string(textDocument), "bqls://") | ||
if !ok { | ||
return VirtualTextDocumentInfo{}, errors.New("invalid text document URI") | ||
} | ||
|
||
result := VirtualTextDocumentInfo{} | ||
for suffix != "" { | ||
for _, prefix := range []string{"project/", "dataset/", "table/", "job/"} { | ||
after, ok := strings.CutPrefix(suffix, prefix) | ||
if !ok { | ||
continue | ||
} | ||
|
||
ind := strings.Index(after, "/") | ||
var val string | ||
if ind == -1 { | ||
val = after | ||
suffix = "" | ||
} else { | ||
val = after[:ind] | ||
suffix = after[ind+1:] | ||
} | ||
|
||
if prefix == "project/" { | ||
result.ProjectID = val | ||
} else if prefix == "dataset/" { | ||
result.DatasetID = val | ||
} else if prefix == "table/" { | ||
result.TableID = val | ||
} else if prefix == "job/" { | ||
result.JobID = val | ||
} | ||
} | ||
} | ||
|
||
if err := result.validate(); err != nil { | ||
return VirtualTextDocumentInfo{}, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (v VirtualTextDocumentInfo) validate() error { | ||
if v.ProjectID == "" { | ||
return errors.New("project ID is required") | ||
} | ||
|
||
if v.DatasetID != "" && v.TableID != "" { | ||
return nil | ||
} | ||
|
||
if v.JobID != "" { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("either dataset ID and table ID or job ID is required") | ||
} | ||
|
||
func newJobVirtualTextDocumentURI(projectID, jobID string) lsp.DocumentURI { | ||
return lsp.DocumentURI(fmt.Sprintf("bqls://project/%s/job/%s", projectID, jobID)) | ||
} | ||
|
||
func newTableVirtualTextDocumentURI(projectID, datasetID, tableID string) lsp.DocumentURI { | ||
return lsp.DocumentURI(fmt.Sprintf("bqls://project/%s/dataset/%s/table/%s", projectID, datasetID, tableID)) | ||
} | ||
|
||
func (h *Handler) handleVirtualTextDocument(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) { | ||
if req.Params == nil { | ||
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams} | ||
} | ||
|
||
var params VirtualTextDocumentParams | ||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil { | ||
return nil, err | ||
} | ||
|
||
virtualTextDocument, err := ParseVirtualTextDocument(params.TextDocument.URI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if virtualTextDocument.TableID != "" { | ||
marks, err := h.project.GetTableInfo(ctx, virtualTextDocument.ProjectID, virtualTextDocument.DatasetID, virtualTextDocument.TableID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return VirtualTextDocument{Contents: marks}, nil | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package langserver_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/kitagry/bqls/langserver" | ||
"github.com/kitagry/bqls/langserver/internal/lsp" | ||
) | ||
|
||
func TestParseVirtualTextDocument(t *testing.T) { | ||
tests := map[string]struct { | ||
uri string | ||
expected langserver.VirtualTextDocumentInfo | ||
expectedErr error | ||
}{ | ||
"Parse project/dataset/table": { | ||
uri: "bqls://project/p/dataset/d/table/t", | ||
expected: langserver.VirtualTextDocumentInfo{ | ||
ProjectID: "p", | ||
DatasetID: "d", | ||
TableID: "t", | ||
}, | ||
}, | ||
"Parse project job": { | ||
uri: "bqls://project/p/job/j", | ||
expected: langserver.VirtualTextDocumentInfo{ | ||
ProjectID: "p", | ||
JobID: "j", | ||
}, | ||
}, | ||
} | ||
|
||
for n, tt := range tests { | ||
t.Run(n, func(t *testing.T) { | ||
got, err := langserver.ParseVirtualTextDocument(lsp.DocumentURI(tt.uri)) | ||
if err != tt.expectedErr { | ||
t.Fatalf("ParseVirtualTextDocument error expected %v, got %v", tt.expectedErr, err) | ||
} | ||
|
||
if diff := cmp.Diff(tt.expected, got); diff != "" { | ||
t.Errorf("ParseVirtualTextDocument result diff (-expect, +got)\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |