-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from lighttiger2505/add-rename
feat: Add text document rename
- Loading branch information
Showing
6 changed files
with
324 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/lighttiger2505/sqls/ast" | ||
"github.com/lighttiger2505/sqls/ast/astutil" | ||
"github.com/lighttiger2505/sqls/internal/lsp" | ||
"github.com/lighttiger2505/sqls/parser" | ||
"github.com/lighttiger2505/sqls/parser/parseutil" | ||
"github.com/lighttiger2505/sqls/token" | ||
"github.com/sourcegraph/jsonrpc2" | ||
) | ||
|
||
func (s *Server) handleTextDocumentRename(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) { | ||
if req.Params == nil { | ||
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams} | ||
} | ||
|
||
var params lsp.RenameParams | ||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil { | ||
return nil, err | ||
} | ||
|
||
f, ok := s.files[params.TextDocument.URI] | ||
if !ok { | ||
return nil, fmt.Errorf("document not found: %s", params.TextDocument.URI) | ||
} | ||
|
||
res, err := rename(f.Text, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
func rename(text string, params lsp.RenameParams) (*lsp.WorkspaceEdit, error) { | ||
parsed, err := parser.Parse(text) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pos := token.Pos{ | ||
Line: params.Position.Line, | ||
Col: params.Position.Character, | ||
} | ||
|
||
// Get the identifer on focus | ||
nodeWalker := parseutil.NewNodeWalker(parsed, pos) | ||
m := astutil.NodeMatcher{ | ||
NodeTypes: []ast.NodeType{ast.TypeIdentifer}, | ||
} | ||
currentVariable := nodeWalker.CurNodeButtomMatched(m) | ||
if currentVariable == nil { | ||
return nil, nil | ||
} | ||
|
||
// Get all identifiers in the statement | ||
idents, err := parseutil.ExtractIdenfiers(parsed, pos) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Extract only those with matching names | ||
renameTarget := []ast.Node{} | ||
for _, ident := range idents { | ||
if ident.String() == currentVariable.String() { | ||
renameTarget = append(renameTarget, ident) | ||
} | ||
} | ||
if len(renameTarget) == 0 { | ||
return nil, nil | ||
} | ||
|
||
edits := make([]lsp.TextEdit, len(renameTarget)) | ||
for i, target := range renameTarget { | ||
edit := lsp.TextEdit{ | ||
Range: lsp.Range{ | ||
Start: lsp.Position{ | ||
Line: target.Pos().Line, | ||
Character: target.Pos().Col, | ||
}, | ||
End: lsp.Position{ | ||
Line: target.End().Line, | ||
Character: target.End().Col, | ||
}, | ||
}, | ||
NewText: params.NewName, | ||
} | ||
edits[i] = edit | ||
} | ||
|
||
res := &lsp.WorkspaceEdit{ | ||
DocumentChanges: []lsp.TextDocumentEdit{ | ||
{ | ||
TextDocument: lsp.OptionalVersionedTextDocumentIdentifier{ | ||
Version: 0, | ||
TextDocumentIdentifier: lsp.TextDocumentIdentifier{ | ||
URI: params.TextDocument.URI, | ||
}, | ||
}, | ||
Edits: edits, | ||
}, | ||
}, | ||
} | ||
|
||
return res, 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,118 @@ | ||
package handler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/lighttiger2505/sqls/internal/config" | ||
"github.com/lighttiger2505/sqls/internal/database" | ||
"github.com/lighttiger2505/sqls/internal/lsp" | ||
) | ||
|
||
var renameTestCases = []struct { | ||
name string | ||
input string | ||
newName string | ||
output lsp.WorkspaceEdit | ||
pos lsp.Position | ||
}{ | ||
{ | ||
name: "ok", | ||
input: "SELECT ci.ID, ci.Name FROM city as ci", | ||
newName: "ct", | ||
output: lsp.WorkspaceEdit{ | ||
DocumentChanges: []lsp.TextDocumentEdit{ | ||
{ | ||
TextDocument: lsp.OptionalVersionedTextDocumentIdentifier{ | ||
Version: 0, | ||
TextDocumentIdentifier: lsp.TextDocumentIdentifier{ | ||
URI: "file:///Users/octref/Code/css-test/test.sql", | ||
}, | ||
}, | ||
Edits: []lsp.TextEdit{ | ||
{ | ||
Range: lsp.Range{ | ||
Start: lsp.Position{ | ||
Line: 0, | ||
Character: 7, | ||
}, | ||
End: lsp.Position{ | ||
Line: 0, | ||
Character: 9, | ||
}, | ||
}, | ||
NewText: "ct", | ||
}, | ||
{ | ||
Range: lsp.Range{ | ||
Start: lsp.Position{ | ||
Line: 0, | ||
Character: 14, | ||
}, | ||
End: lsp.Position{ | ||
Line: 0, | ||
Character: 16, | ||
}, | ||
}, | ||
NewText: "ct", | ||
}, | ||
{ | ||
Range: lsp.Range{ | ||
Start: lsp.Position{ | ||
Line: 0, | ||
Character: 35, | ||
}, | ||
End: lsp.Position{ | ||
Line: 0, | ||
Character: 37, | ||
}, | ||
}, | ||
NewText: "ct", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
pos: lsp.Position{ | ||
Line: 0, | ||
Character: 8, | ||
}, | ||
}, | ||
} | ||
|
||
func TestRenameMain(t *testing.T) { | ||
tx := newTestContext() | ||
tx.setup(t) | ||
defer tx.tearDown() | ||
|
||
cfg := &config.Config{ | ||
Connections: []*database.DBConfig{ | ||
{Driver: "mock"}, | ||
}, | ||
} | ||
tx.addWorkspaceConfig(t, cfg) | ||
|
||
for _, tt := range renameTestCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tx.textDocumentDidOpen(t, testFileURI, tt.input) | ||
|
||
params := lsp.RenameParams{ | ||
TextDocument: lsp.TextDocumentIdentifier{ | ||
URI: testFileURI, | ||
}, | ||
Position: tt.pos, | ||
NewName: tt.newName, | ||
} | ||
var got lsp.WorkspaceEdit | ||
err := tx.conn.Call(tx.ctx, "textDocument/rename", params, &got) | ||
if err != nil { | ||
t.Errorf("conn.Call textDocument/rename: %+v", err) | ||
return | ||
} | ||
|
||
if diff := cmp.Diff(tt.output, got); diff != "" { | ||
t.Errorf("unmatch rename edits (- want, + got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
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,25 @@ | ||
package parseutil | ||
|
||
import ( | ||
"github.com/lighttiger2505/sqls/ast" | ||
"github.com/lighttiger2505/sqls/ast/astutil" | ||
"github.com/lighttiger2505/sqls/token" | ||
) | ||
|
||
func ExtractIdenfiers(parsed ast.TokenList, pos token.Pos) ([]ast.Node, error) { | ||
stmt, err := extractFocusedStatement(parsed, pos) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
identiferMatcher := astutil.NodeMatcher{ | ||
NodeTypes: []ast.NodeType{ | ||
ast.TypeIdentifer, | ||
}, | ||
} | ||
return parsePrefix(astutil.NewNodeReader(stmt), identiferMatcher, parseIdentifer), nil | ||
} | ||
|
||
func parseIdentifer(reader *astutil.NodeReader) []ast.Node { | ||
return []ast.Node{reader.CurNode} | ||
} |