Skip to content

Commit

Permalink
Add ability to switch between files in Git Diff widget (#5965)
Browse files Browse the repository at this point in the history
* Performs small refactoring of git-compare-related functionality.
* Adds ability to switch to the next/previous file in git compare widget.
* Adds hotkeys for next and previous diff
* Adds Save Changes button for git compare widget.
* Fixes compare with deleted file bug.
* Moves Git Diff widget from iframe to IDE.
  • Loading branch information
mmorhun authored Sep 12, 2017
1 parent aaaf6a9 commit c7ed1d8
Show file tree
Hide file tree
Showing 33 changed files with 949 additions and 485 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
@def dividerThickness 1px;
@def popupPadding 24px;
@def titlePaddingBottom 16px;
@def buttonSpacing 18px;
@def buttonSideSpacing 9px;
@def labelPaddingBottom 6px;
@def examplePaddingTop 4px;
@def formButtonPaddingTop 18px;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.ide.ui.button;

/**
* Describes button alignment in a horizontal container.
*
* @author Mykola Morhun
*/
public enum ButtonAlignment {
LEFT,
RIGHT
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.gwt.user.client.ui.Widget;
import elemental.js.dom.JsElement;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.ui.button.ButtonAlignment;
import org.vectomatic.dom.svg.ui.SVGResource;

/**
Expand Down Expand Up @@ -154,20 +155,32 @@ public void setHideOnEscapeEnabled(boolean isEnabled) {
}

protected Button createButton(String title, String debugId, ClickHandler clickHandler) {
return createButton(title, debugId, clickHandler, ButtonAlignment.RIGHT);
}

protected Button createButton(
String title, String debugId, ClickHandler clickHandler, ButtonAlignment alignment) {
Button button = new Button();
button.setText(title);
button.ensureDebugId(debugId);
button.getElement().setId(debugId);
button.addStyleName(resources.windowCss().button());
addButtonAlignment(button, alignment);
button.addClickHandler(clickHandler);
//set default tab index
button.setTabIndex(0);
return button;
}

protected Button createPrimaryButton(String title, String debugId, ClickHandler clickHandler) {
return createPrimaryButton(title, debugId, clickHandler, ButtonAlignment.RIGHT);
}

protected Button createPrimaryButton(
String title, String debugId, ClickHandler clickHandler, ButtonAlignment alignment) {
Button button = createButton(title, debugId, clickHandler);
button.addStyleName(resources.windowCss().primaryButton());
addButtonAlignment(button, alignment);
//set default tab index
button.setTabIndex(0);
return button;
Expand Down Expand Up @@ -243,6 +256,17 @@ public void execute() {
}
}

private void addButtonAlignment(Button button, ButtonAlignment alignment) {
switch (alignment) {
case LEFT:
button.addStyleName(resources.windowCss().buttonAlignLeft());
break;
case RIGHT:
default:
button.addStyleName(resources.windowCss().buttonAlignRight());
}
}

/**
* Sets focus on the given element. If {@code elementToFocus} is {@code null}, no element will be
* given focus
Expand Down Expand Up @@ -343,6 +367,10 @@ public interface Css extends CssResource {

String button();

String buttonAlignLeft();

String buttonAlignRight();

String image();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
}

.alignBtn {
float: right;
margin-left: buttonSpacing;
margin-left: buttonSideSpacing;
margin-right: buttonSideSpacing;
}

.headerTitleWrapper {
Expand Down Expand Up @@ -151,7 +151,6 @@
}

.button, .primaryButton {
float: right;
height: buttonHeight;
line-height: buttonLineHeight;
min-width: buttonMinWidth;
Expand Down Expand Up @@ -194,6 +193,14 @@
opacity: 0.5;
}

.buttonAlignLeft {
float: left;
}

.buttonAlignRight {
float: right;
}

/* Blue button styles */
.primaryButton {
background: primaryButtonBackground;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.eclipse.che.ide.ext.git.client.action.FetchAction;
import org.eclipse.che.ide.ext.git.client.action.HistoryAction;
import org.eclipse.che.ide.ext.git.client.action.InitRepositoryAction;
import org.eclipse.che.ide.ext.git.client.action.NextDiffAction;
import org.eclipse.che.ide.ext.git.client.action.PreviousDiffAction;
import org.eclipse.che.ide.ext.git.client.action.PullAction;
import org.eclipse.che.ide.ext.git.client.action.PushAction;
import org.eclipse.che.ide.ext.git.client.action.RemoveFromIndexAction;
Expand All @@ -57,6 +59,9 @@ public class GitExtension {
public static final String HISTORY_GROUP_MAIN_MENU = "GitHistoryGroup";
public static final String GIT_COMPARE_WITH_LATEST = "gitCompareWithLatest";

public static final String NEXT_DIFF_ACTION_ID = "nextDiff";
public static final String PREV_DIFF_ACTION_ID = "prevDiff";

@Inject
public GitExtension(
GitResources resources,
Expand All @@ -81,6 +86,8 @@ public GitExtension(
CompareWithLatestAction compareWithLatestAction,
CompareWithBranchAction compareWithBranchAction,
CompareWithRevisionAction compareWithRevisionAction,
NextDiffAction nextDiffAction,
PreviousDiffAction previousDiffAction,
KeyBindingAgent keyBinding,
AppContext appContext) {

Expand Down Expand Up @@ -180,8 +187,18 @@ public GitExtension(
editorContextMenuGroup.addSeparator();
editorContextMenuGroup.add(gitContextMenuGroup);

actionManager.registerAction(NEXT_DIFF_ACTION_ID, nextDiffAction);
actionManager.registerAction(PREV_DIFF_ACTION_ID, previousDiffAction);

keyBinding
.getGlobal()
.addKey(new KeyBuilder().action().alt().charCode('d').build(), GIT_COMPARE_WITH_LATEST);

keyBinding
.getGlobal()
.addKey(new KeyBuilder().alt().charCode('.').build(), NEXT_DIFF_ACTION_ID);
keyBinding
.getGlobal()
.addKey(new KeyBuilder().alt().charCode(',').build(), PREV_DIFF_ACTION_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public interface GitLocalizationConstant extends Messages {
@Key("button.compare")
String buttonCompare();

@Key("button.save_changes")
String buttonSaveChanges();

@Key("button.next_diff")
String buttonNextDiff();

@Key("button.previous_diff")
String buttonPreviousDiff();

@Key("console.tooltip.clear")
String buttonClear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
import static org.eclipse.che.api.git.shared.DiffType.NAME_STATUS;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.NOT_EMERGE_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
import static org.eclipse.che.ide.ext.git.client.compare.FileStatus.defineStatus;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
Expand All @@ -29,8 +26,8 @@
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.ext.git.client.GitLocalizationConstant;
import org.eclipse.che.ide.ext.git.client.compare.AlteredFiles;
import org.eclipse.che.ide.ext.git.client.compare.ComparePresenter;
import org.eclipse.che.ide.ext.git.client.compare.FileStatus.Status;
import org.eclipse.che.ide.ext.git.client.compare.changeslist.ChangesListPresenter;

/**
Expand Down Expand Up @@ -106,25 +103,13 @@ public void actionPerformed(ActionEvent e) {
null)
.show();
} else {
final String[] changedFiles = diff.split("\n");
if (changedFiles.length == 1) {
project
.getFile(changedFiles[0].substring(2))
.then(
file -> {
if (file.isPresent()) {
comparePresenter.showCompareWithLatest(
file.get(),
defineStatus(changedFiles[0].substring(0, 1)),
REVISION);
}
});
AlteredFiles alteredFiles = new AlteredFiles(project, diff);
if (alteredFiles.getFilesQuantity() == 1) {

comparePresenter.showCompareWithLatest(alteredFiles, null, REVISION);
} else {
Map<String, Status> items = new HashMap<>();
for (String item : changedFiles) {
items.put(item.substring(2, item.length()), defineStatus(item.substring(0, 1)));
}
changesListPresenter.show(items, REVISION, null, project);

changesListPresenter.show(alteredFiles, REVISION, null);
}
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.ide.ext.git.client.action;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.che.ide.api.action.Action;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.ext.git.client.compare.ComparePresenter;

/** @author Mykola Morhun */
@Singleton
public class NextDiffAction extends Action {

private final ComparePresenter comparePresenter;

@Inject
public NextDiffAction(ComparePresenter comparePresenter) {
this.comparePresenter = comparePresenter;
}

@Override
public void actionPerformed(ActionEvent e) {
if (comparePresenter.isShown()) {
comparePresenter.onNextDiffClicked();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.ide.ext.git.client.action;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.che.ide.api.action.Action;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.ext.git.client.compare.ComparePresenter;

/** @author Mykola Morhun */
@Singleton
public class PreviousDiffAction extends Action {

private final ComparePresenter comparePresenter;

@Inject
public PreviousDiffAction(ComparePresenter comparePresenter) {
super(null, null);
this.comparePresenter = comparePresenter;
}

@Override
public void actionPerformed(ActionEvent e) {
if (comparePresenter.isShown()) {
comparePresenter.onPreviousDiffClicked();
}
}
}
Loading

0 comments on commit c7ed1d8

Please sign in to comment.