Skip to content

x/tools/gopls: fix rename command line fail #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions gopls/internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,21 @@ func (cli *cmdClient) applyWorkspaceEdit(wsedit *protocol.WorkspaceEdit) error {

case c.RenameFile != nil:
// Analyze as creation + deletion. (NB: loses file mode.)
f := cli.openFile(c.RenameFile.OldURI)
if f.err != nil {
return f.err
}
if err := create(c.RenameFile.NewURI, f.mapper.Content); err != nil {
fs, err := cli.openFiles(c.RenameFile.OldURI)
if err != nil {
return err
}
if err := delete(f.mapper.URI, f.mapper.Content); err != nil {
return err
for _, f := range fs {
if f.err != nil {
return f.err
}
fileName := protocol.DocumentURI(filepath.Join(string(c.RenameFile.NewURI), filepath.Base(f.uri.Path())))
if err := create(fileName, f.mapper.Content); err != nil {
return err
}
if err := delete(f.mapper.URI, f.mapper.Content); err != nil {
return err
}
}

case c.DeleteFile != nil:
Expand Down Expand Up @@ -788,6 +794,32 @@ func (c *cmdClient) openFile(uri protocol.DocumentURI) *cmdFile {
return c.getFile(uri)
}

func (c *cmdClient) openFiles(uri protocol.DocumentURI) ([]*cmdFile, error) {
c.filesMu.Lock()
defer c.filesMu.Unlock()
s, err := os.Stat(uri.Path())
if err != nil {
return nil, err
}

if s.IsDir() {
entries, err := os.ReadDir(uri.Path())
if err != nil {
return nil, err
}
files := make([]*cmdFile, 0)
for _, e := range entries {
if !e.IsDir() {
files = append(files, c.getFile(protocol.DocumentURI(filepath.Join(string(uri), e.Name()))))
}
}

return files, nil
}

return []*cmdFile{c.getFile(uri)}, nil
}

// TODO(adonovan): provide convenience helpers to:
// - map a (URI, protocol.Range) to a MappedRange;
// - parse a command-line argument to a MappedRange.
Expand Down