Skip to content

Commit

Permalink
language-server: now renames using official ResourceOperation API ins…
Browse files Browse the repository at this point in the history
…tead of renaming with Java (references #3)
  • Loading branch information
joshtynjala committed Dec 5, 2018
1 parent fb9b904 commit 119fafb
Showing 1 changed file with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,16 @@
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameFile;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.ResourceOperation;
import org.eclipse.lsp4j.ResourceOperationKind;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SignatureInformation;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
import org.eclipse.lsp4j.TextDocumentEdit;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentItem;
import org.eclipse.lsp4j.TextDocumentPositionParams;
Expand Down Expand Up @@ -3850,7 +3854,7 @@ else if (offsetNode instanceof IIdentifierNode)
return new WorkspaceEdit(new HashMap<>());
}

WorkspaceEdit result = renameDefinition(definition, params.getNewName(), project);
WorkspaceEdit result = renameDefinition(params, definition, project);
return result;
}

Expand All @@ -3864,7 +3868,7 @@ private WorkspaceEdit mxmlRename(RenameParams params, RoyaleProject project, IMX
//ignore the tag's prefix
return new WorkspaceEdit(new HashMap<>());
}
WorkspaceEdit result = renameDefinition(definition, params.getNewName(), project);
WorkspaceEdit result = renameDefinition(params, definition, project);
return result;
}

Expand Down Expand Up @@ -5424,7 +5428,7 @@ private IFunctionCallNode getAncestorFunctionCallNode(IASNode offsetNode)
return null;
}

private WorkspaceEdit renameDefinition(IDefinition definition, String newName, RoyaleProject project)
private WorkspaceEdit renameDefinition(RenameParams params, IDefinition definition, RoyaleProject project)
{
if (definition == null)
{
Expand All @@ -5438,8 +5442,8 @@ private WorkspaceEdit renameDefinition(IDefinition definition, String newName, R
return new WorkspaceEdit(new HashMap<>());
}
WorkspaceEdit result = new WorkspaceEdit();
Map<String, List<TextEdit>> changes = new HashMap<>();
result.setChanges(changes);
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();
result.setDocumentChanges(documentChanges);
if (definition.getContainingFilePath().endsWith(SWC_EXTENSION))
{
if (languageClient != null)
Expand All @@ -5462,6 +5466,7 @@ private WorkspaceEdit renameDefinition(IDefinition definition, String newName, R
}
return result;
}
String newName = params.getNewName();
Path originalDefinitionFilePath = null;
Path newDefinitionFilePath = null;
for (ICompilationUnit compilationUnit : project.getCompilationUnits())
Expand Down Expand Up @@ -5531,28 +5536,25 @@ private WorkspaceEdit renameDefinition(IDefinition definition, String newName, R
continue;
}

URI uri = Paths.get(compilationUnit.getAbsoluteFilename()).toUri();
Path textDocumentPath = Paths.get(compilationUnit.getAbsoluteFilename());
if (definitionIsMainDefinitionInCompilationUnit(compilationUnit, definition))
{
originalDefinitionFilePath = Paths.get(compilationUnit.getAbsoluteFilename());
originalDefinitionFilePath = textDocumentPath;
String newBaseName = newName + "." + Files.getFileExtension(originalDefinitionFilePath.toFile().getName());
newDefinitionFilePath = originalDefinitionFilePath.getParent().resolve(newBaseName);
uri = newDefinitionFilePath.toUri();
}
changes.put(uri.toString(), textEdits);

VersionedTextDocumentIdentifier versionedIdentifier =
new VersionedTextDocumentIdentifier(textDocumentPath.toUri().toString(), null);
TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedIdentifier, textEdits);
documentChanges.add(Either.forLeft(textDocumentEdit));
}
if (newDefinitionFilePath != null)
{
//wait to actually rename the file because we need to be sure
//that finding the identifiers above still works with the old name
try
{
java.nio.file.Files.move(originalDefinitionFilePath, newDefinitionFilePath, StandardCopyOption.ATOMIC_MOVE);
}
catch(IOException e)
{
System.err.println("could not move file for rename: " + newDefinitionFilePath.toUri().toString());
}
RenameFile renameFile = new RenameFile();
renameFile.setOldUri(originalDefinitionFilePath.toUri().toString());
renameFile.setNewUri(newDefinitionFilePath.toUri().toString());
documentChanges.add(Either.forRight(renameFile));
}
return result;
}
Expand Down

0 comments on commit 119fafb

Please sign in to comment.