Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Svydenko <vsvydenk@redhat.com>
  • Loading branch information
svor committed May 12, 2018
1 parent 7618817 commit 31d6ffb
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@
*/
package org.eclipse.che.ide.ext.java.client.refactoring.preview;

import static java.util.stream.Collectors.toList;

import com.google.common.base.Optional;
import com.google.gwt.dom.client.Document;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.che.api.languageserver.util.URIUtil;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.resources.Container;
Expand All @@ -38,6 +30,14 @@
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
* @author Dmitry Shnurenko
* @author Valeriy Svydenko
Expand Down Expand Up @@ -125,7 +125,7 @@ public void onSelectionChanged(PreviewNode selectedNode) {
}

private void updateContentInCompareWidget(PreviewNode selectedNode, List<TextEdit> edits) {
String path = URIUtil.removePrefixUri(selectedNode.getUri());
String path = selectedNode.getUri();
Container workspaceRoot = appContext.getWorkspaceRoot();
Promise<Optional<File>> file = workspaceRoot.getFile(path);
file.then(
Expand Down Expand Up @@ -239,18 +239,16 @@ private void prepareResourceChangeNodes(List<ResourceChange> resourceChanges) {
+ Path.valueOf(newUri).lastSegment()
+ "'");
} else {
String pathString = URIUtil.removePrefixUri(newUri);
node.setDescription(
"Move resource '"
+ Path.valueOf(current).lastSegment()
+ "' to '"
+ Path.valueOf(pathString).removeLastSegments(1)
+ Path.valueOf(newUri).removeLastSegments(1)
+ "'");
}
fileNodes.put(newUri, node);
} else if (current == null && newUri != null) {
String pathString = URIUtil.removePrefixUri(newUri);
node.setDescription("Create resource: '" + Path.valueOf(pathString) + "'");
node.setDescription("Create resource: '" + Path.valueOf(newUri) + "'");
fileNodes.put(newUri, node);
}
}
Expand All @@ -263,7 +261,7 @@ private void prepareTextEditNodes(Map<String, List<TextEdit>> changes) {
parent.setEnable(true);
String uniqueId = Document.get().createUniqueId();
parent.setId(uniqueId);
Path path = Path.valueOf(URIUtil.removePrefixUri(uri));
Path path = Path.valueOf(uri);
parent.setDescription(path.lastSegment() + " - " + path.removeLastSegments(1));
fileNodes.put(uri, parent);
for (TextEdit change : changes.get(uri)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@
*/
package org.eclipse.che.ide.ext.java.client.refactoring.rename;

import static org.eclipse.che.ide.api.editor.events.FileEvent.FileOperation.CLOSE;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.che.ide.api.editor.EditorWithAutoSave;
import org.eclipse.che.ide.api.editor.document.Document;
import org.eclipse.che.ide.api.editor.events.FileEvent;
import org.eclipse.che.ide.api.editor.events.FileEvent.FileEventHandler;
import org.eclipse.che.ide.api.editor.link.HasLinkedMode;
Expand All @@ -42,23 +36,28 @@
import org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard.RenamePresenter;
import org.eclipse.che.ide.ext.java.client.service.JavaLanguageExtensionServiceClient;
import org.eclipse.che.ide.ui.dialogs.DialogFactory;
import org.eclipse.che.jdt.ls.extension.api.RenameType;
import org.eclipse.che.jdt.ls.extension.api.dto.LinkedData;
import org.eclipse.che.jdt.ls.extension.api.dto.LinkedModeModel;
import org.eclipse.che.jdt.ls.extension.api.dto.LinkedModelParams;
import org.eclipse.che.jdt.ls.extension.api.dto.LinkedPositionGroup;
import org.eclipse.che.jdt.ls.extension.api.dto.Region;
import org.eclipse.che.jdt.ls.extension.api.RenameKind;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSettings;
import org.eclipse.che.plugin.languageserver.ide.editor.quickassist.ApplyWorkspaceEditAction;
import org.eclipse.che.plugin.languageserver.ide.service.TextDocumentServiceClient;
import org.eclipse.che.plugin.languageserver.ide.util.DtoBuildHelper;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.ResourceChange;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static org.eclipse.che.ide.api.editor.events.FileEvent.FileOperation.CLOSE;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;

/**
* Class for rename refactoring java classes
*
Expand Down Expand Up @@ -156,25 +155,26 @@ private void showError() {

private void createLinkedRename() {
cursorPosition = textEditor.getCursorPosition();
LinkedModelParams params = dtoFactory.createDto(LinkedModelParams.class);
params.setUri(textEditor.getDocument().getFile().getLocation().toString());
params.setOffset(textEditor.getCursorOffset());

Document document = textEditor.getDocument();
TextDocumentPositionParams params =
dtoBuildHelper.createTDPP(document, textEditor.getCursorOffset());

extensionServiceClient
.getLinkedModeModel(params)
.then(
linkedModeModel -> {
ranges -> {
clientServerEventService
.sendFileTrackingSuspendEvent()
.then(
success -> {
if (linkedModeModel == null) {
if (ranges == null || ranges.isEmpty()) {
showError();
isActiveLinkedEditor = false;
clientServerEventService.sendFileTrackingResumeEvent();
return;
}
activateLinkedModeIntoEditor(linkedModeModel);
activateLinkedModeIntoEditor(ranges);
});
})
.catchError(
Expand All @@ -199,27 +199,21 @@ public boolean isActiveLinkedEditor() {
return isActiveLinkedEditor;
}

private void activateLinkedModeIntoEditor(LinkedModeModel linkedModeModel) {
private void activateLinkedModeIntoEditor(List<Range> ranges) {
sendCloseEvent();
mode = linkedEditor.getLinkedMode();
LinkedModel model = linkedEditor.createLinkedModel();
List<LinkedModelGroup> groups = new ArrayList<>();
for (LinkedPositionGroup positionGroup : linkedModeModel.getGroups()) {
LinkedModelGroup group = linkedEditor.createLinkedGroup();
LinkedData data = positionGroup.getLinkedData();
if (data != null) {
LinkedModelData modelData = linkedEditor.createLinkedModelData();
modelData.setType("link");
modelData.setValues(data.getValues());
group.setData(modelData);
}
List<Position> positions = new ArrayList<>();
for (Region region : positionGroup.getPositions()) {
positions.add(new Position(region.getOffset(), region.getLength()));
}
group.setPositions(positions);
groups.add(group);
LinkedModelGroup group = linkedEditor.createLinkedGroup();
List<Position> positions = new ArrayList<>();
for (Range range : ranges) {
LinkedModelData modelData = linkedEditor.createLinkedModelData();
modelData.setType("link");
group.setData(modelData);
positions.add(createPositionFromRange(range, textEditor.getDocument()));
}
group.setPositions(positions);
groups.add(group);
model.setGroups(groups);
disableAutoSave();

Expand All @@ -242,7 +236,7 @@ public void onLinkedModeExited(boolean successful, int start, int end) {
isActiveLinkedEditor = false;

boolean isNameChanged = start >= 0 && end >= 0;
if (!isSuccessful && isNameChanged) {
if (!isSuccessful && isNameChanged && textEditor.isDirty()) {
undoChanges();
}

Expand All @@ -259,6 +253,33 @@ public void onLinkedModeExited(boolean successful, int start, int end) {
});
}

private Position createPositionFromRange(Range range, Document document) {
int start =
document.getIndexFromPosition(
new TextPosition(range.getStart().getLine(), range.getStart().getCharacter()));
int end =
document.getIndexFromPosition(
new TextPosition(range.getEnd().getLine(), range.getEnd().getCharacter()));

if (start == -1 && end == -1) {
return new Position(0);
}

if (start == -1) {
return new Position(end);
}

if (end == -1) {
return new Position(start);
}

int length = end - start;
if (length < 0) {
return null;
}
return new Position(start, length);
}

private void sendCloseEvent() {
TextDocumentIdentifier documentId = dtoHelper.createTDI(textEditor.getEditorInput().getFile());
DidCloseTextDocumentParams closeEvent = dtoFactory.createDto(DidCloseTextDocumentParams.class);
Expand Down Expand Up @@ -286,7 +307,7 @@ private void performRename(String newName) {

settings.setUpdateReferences(true);
settings.setRenameParams(renameParams);
settings.setRenameType(RenameType.JAVA_ELEMENT);
settings.setRenameKind(RenameKind.JAVA_ELEMENT);

extensionServiceClient
.rename(settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
*/
package org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard;

import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.LinkedList;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.editor.EditorPartPresenter;
Expand All @@ -33,11 +29,11 @@
import org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard.RenameView.ActionDelegate;
import org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard.similarnames.SimilarNamesConfigurationPresenter;
import org.eclipse.che.ide.ext.java.client.service.JavaLanguageExtensionServiceClient;
import org.eclipse.che.jdt.ls.extension.api.RenameType;
import org.eclipse.che.jdt.ls.extension.api.RenameKind;
import org.eclipse.che.jdt.ls.extension.api.dto.CheWorkspaceEdit;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSelectionParams;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSettings;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameWizardType;
import org.eclipse.che.jdt.ls.extension.api.dto.RenamingElementInfo;
import org.eclipse.che.plugin.languageserver.ide.editor.quickassist.ApplyWorkspaceEditAction;
import org.eclipse.che.plugin.languageserver.ide.util.DtoBuildHelper;
import org.eclipse.lsp4j.RenameParams;
Expand All @@ -46,6 +42,11 @@
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import java.util.LinkedList;

import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;

/**
* The class that manages Rename panel widget.
*
Expand Down Expand Up @@ -111,15 +112,15 @@ public void show(RefactorInfo refactorInfo) {
String location =
editorAgent.getActiveEditor().getEditorInput().getFile().getLocation().toString();
params.setResourceUri(location);
params.setRenameType(RenameType.JAVA_ELEMENT);
params.setRenameKind(RenameKind.JAVA_ELEMENT);
} else {
// get selected resource
Resource resource = refactorInfo.getResources()[0];
params.setResourceUri(resource.getLocation().toString());
if (RefactoredItemType.COMPILATION_UNIT.equals(refactorInfo.getRefactoredItemType())) {
params.setRenameType(RenameType.COMPILATION_UNIT);
params.setRenameKind(RenameKind.COMPILATION_UNIT);
} else {
params.setRenameType(RenameType.PACKAGE);
params.setRenameKind(RenameKind.PACKAGE);
}
}

Expand All @@ -133,10 +134,10 @@ public void show(RefactorInfo refactorInfo) {
});
}

private void showWizard(RenameWizardType renameWizard) {
prepareWizard(renameWizard.getElementName());
private void showWizard(RenamingElementInfo elementInfo) {
prepareWizard(elementInfo.getElementName());

switch (renameWizard.getRenameType()) {
switch (elementInfo.getRenameKind()) {
case COMPILATION_UNIT:
view.setTitleCaption(locale.renameCompilationUnitTitle());
view.setVisiblePatternsPanel(true);
Expand Down Expand Up @@ -228,15 +229,15 @@ public void validateName() {
position.setCharacter(cursorPosition.getCharacter());
position.setLine(cursorPosition.getLine());
params.setPosition(position);
params.setRenameType(RenameType.JAVA_ELEMENT);
params.setRenameKind(RenameKind.JAVA_ELEMENT);
String location =
editorAgent.getActiveEditor().getEditorInput().getFile().getLocation().toString();
params.setResourceUri(location);
} else if (RefactoredItemType.COMPILATION_UNIT.equals(refactorInfo.getRefactoredItemType())) {
params.setRenameType(RenameType.COMPILATION_UNIT);
params.setRenameKind(RenameKind.COMPILATION_UNIT);
params.setResourceUri(refactorInfo.getResources()[0].getLocation().toString());
} else if (RefactoredItemType.PACKAGE.equals(refactorInfo.getRefactoredItemType())) {
params.setRenameType(RenameType.PACKAGE);
params.setRenameKind(RenameKind.PACKAGE);
params.setResourceUri(refactorInfo.getResources()[0].getLocation().toString());
}

Expand Down Expand Up @@ -313,15 +314,15 @@ private RenameParams createRenameParams(RenameSettings renameSettings) {
position.setCharacter(cursorPosition.getCharacter());
position.setLine(cursorPosition.getLine());
renameParams.setPosition(position);
renameSettings.setRenameType(RenameType.JAVA_ELEMENT);
renameSettings.setRenameKind(RenameKind.JAVA_ELEMENT);
} else if (RefactoredItemType.COMPILATION_UNIT.equals(refactorInfo.getRefactoredItemType())) {
renameSettings.setRenameType(RenameType.COMPILATION_UNIT);
renameSettings.setRenameKind(RenameKind.COMPILATION_UNIT);
TextDocumentIdentifier textDocumentIdentifier =
dtoFactory.createDto(TextDocumentIdentifier.class);
textDocumentIdentifier.setUri(refactorInfo.getResources()[0].getLocation().toString());
renameParams.setTextDocument(textDocumentIdentifier);
} else if (RefactoredItemType.PACKAGE.equals(refactorInfo.getRefactoredItemType())) {
renameSettings.setRenameType(RenameType.PACKAGE);
renameSettings.setRenameKind(RenameKind.PACKAGE);
TextDocumentIdentifier textDocumentIdentifier =
dtoFactory.createDto(TextDocumentIdentifier.class);
textDocumentIdentifier.setUri(refactorInfo.getResources()[0].getLocation().toString());
Expand Down Expand Up @@ -357,8 +358,7 @@ private RenameSettings createRenameSettings() {
renameSettings.setUpdateTextualMatches(view.isUpdateTextualOccurrences());
renameSettings.setUpdateSimilarDeclarations(view.isUpdateSimilarlyVariables());
if (view.isUpdateSimilarlyVariables()) {
renameSettings.setMachStrategy(
similarNamesConfigurationPresenter.getMachStrategy().getValue());
renameSettings.setMatchStrategy(similarNamesConfigurationPresenter.getMatchStrategy());
}

return renameSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard.similarnames.SimilarNamesConfigurationView.ActionDelegate;
import org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameSettings.MachStrategy;
import org.eclipse.che.jdt.ls.extension.api.MatchStrategy;

/**
* The class that manages similar name value.
Expand All @@ -35,8 +35,8 @@ public void show() {
view.showDialog();
}

/** @return selected value of mach strategy. */
public MachStrategy getMachStrategy() {
return view.getMachStrategy();
/** @return selected value of match strategy. */
public MatchStrategy getMatchStrategy() {
return view.getMatchStrategy();
}
}
Loading

0 comments on commit 31d6ffb

Please sign in to comment.