代码拉取完成,页面将自动刷新
package handler
import (
"context"
"encoding/json"
"fmt"
"gitee.com/wenj91/sqls/ast"
"gitee.com/wenj91/sqls/ast/astutil"
"gitee.com/wenj91/sqls/parser"
"gitee.com/wenj91/sqls/parser/parseutil"
"gitee.com/wenj91/sqls/pkg/lsp"
"gitee.com/wenj91/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 identifier on focus
nodeWalker := parseutil.NewNodeWalker(parsed, pos)
m := astutil.NodeMatcher{
NodeTypes: []ast.NodeType{ast.TypeIdentifier},
}
currentVariable := nodeWalker.CurNodeBottomMatched(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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。