Skip to content

The Unified and Autonomous Development Platform

Add goto line feature

robin shinefc7153799b96
diff --git a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java
index 38508debb6..3ac67c3318 100644
--- a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java
+++ b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java
@@ -82,8 +82,10 @@ public abstract class AbstractEntity implements Serializable, Comparable<Abstrac
* @param entity
* @return
*/
- public static @Nullable Long idOf(AbstractEntity entity) {
- if (entity instanceof HibernateProxy) {
+ public static @Nullable Long idOf(@Nullable AbstractEntity entity) {
+ if (entity == null) {
+ return null;
+ } else if (entity instanceof HibernateProxy) {
HibernateProxy proxy = (HibernateProxy) entity;
return (Long) proxy.getHibernateLazyInitializer().getIdentifier();
} else {
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java
index 593c90a682..eb2dd0ee2e 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java
@@ -51,6 +51,7 @@ public class CodeMirrorResourceReference extends WebjarsJavaScriptResourceRefere
JavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("codemirror/current/addon/display/fullscreen.js")),
JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(CodeMirrorResourceReference.class, "annotatescrollbar2.js")),
JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(CodeMirrorResourceReference.class, "simplescrollbars.js")),
+ JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(CodeMirrorResourceReference.class, "gotoline.js")),
JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(CodeMirrorResourceReference.class, "identifier-highlighter.js")),
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/gotoline.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/gotoline.js
new file mode 100644
index 0000000000..6941061d5d
--- /dev/null
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/gotoline.js
@@ -0,0 +1,36 @@
+(function(mod) {
+ if (typeof exports == "object" && typeof module == "object") // CommonJS
+ mod(require("../../lib/codemirror"), require("../dialog/dialog"));
+ else if (typeof define == "function" && define.amd) // AMD
+ define(["../../lib/codemirror", "../dialog/dialog"], mod);
+ else // Plain browser env
+ mod(CodeMirror);
+})(function(CodeMirror) {
+ "use strict";
+
+ function dialog(cm, text, shortText, deflt, f) {
+ if (cm.openDialog) cm.openDialog(text, f, {value: deflt});
+ else f(prompt(shortText, deflt));
+ }
+ function validateQuery(query) {
+ if (/\d+/.test(query)) return true;
+ return false;
+ }
+ var queryDialog =
+ 'Go to Line : <input type="text" style="width: 10em"/> <span style="color: #888">(Press Enter)</span>';
+
+ function jumpToLine(cm) {
+ dialog(cm, queryDialog, "Go to Line:", 0, function(query) {
+ if (validateQuery(query)) {
+ var line = parseInt(query);
+ line = line -1;
+ cm.setCursor({line:line,ch:0});
+ var myHeight = cm.getScrollInfo().clientHeight;
+ var coords = cm.charCoords({line: line, ch: 0}, "local");
+ cm.scrollTo(null, (coords.top + coords.bottom - myHeight) / 2);
+ }
+ });
+ }
+
+ CodeMirror.commands.gotoLine = function(cm) {jumpToLine(cm);};
+});
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/instant/InstantSearchPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/instant/InstantSearchPanel.java
index 34f051f9ba..fb2fde7559 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/instant/InstantSearchPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/instant/InstantSearchPanel.java
@@ -157,17 +157,9 @@ public abstract class InstantSearchPanel extends Panel {
link.add(new Label("scope", hit.getScope()).setVisible(hit.getScope()!=null));
item.add(link);
- Long requestId;
- if (requestModel.getObject() != null)
- requestId = requestModel.getObject().getId();
- else
- requestId = null;
+ Long requestId = PullRequest.idOf(requestModel.getObject());
- Highlight highlight;
- if (hit.getTokenPos() != null)
- highlight = new Highlight(hit.getTokenPos());
- else
- highlight = null;
+ Highlight highlight = Highlight.of(hit.getTokenPos());
PageParameters params = RepoFilePage.paramsOf(
repoModel.getObject(), revisionModel.getObject(),
hit.getBlobPath(), highlight, requestId);
@@ -291,17 +283,8 @@ public abstract class InstantSearchPanel extends Panel {
if (item.getIndex() + symbolHits.size() == activeHitIndex)
item.add(AttributeModifier.append("class", " active"));
- Long requestId;
- if (requestModel.getObject() != null)
- requestId = requestModel.getObject().getId();
- else
- requestId = null;
-
- Highlight highlight;
- if (hit.getTokenPos() != null)
- highlight = new Highlight(hit.getTokenPos());
- else
- highlight = null;
+ Long requestId = PullRequest.idOf(requestModel.getObject());
+ Highlight highlight = Highlight.of(hit.getTokenPos());
PageParameters params = RepoFilePage.paramsOf(
repoModel.getObject(), revisionModel.getObject(),
hit.getBlobPath(), highlight, requestId);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/result/SearchResultPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/result/SearchResultPanel.java
index d82dccf7a4..9a73cad931 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/result/SearchResultPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobsearch/result/SearchResultPanel.java
@@ -38,6 +38,7 @@ import org.eclipse.jgit.lib.FileMode;
import com.pmease.commons.git.BlobIdent;
import com.pmease.commons.wicket.ajaxlistener.ConfirmLeaveListener;
import com.pmease.commons.wicket.assets.hotkeys.HotkeysResourceReference;
+import com.pmease.gitplex.core.model.PullRequest;
import com.pmease.gitplex.search.hit.FileHit;
import com.pmease.gitplex.search.hit.QueryHit;
import com.pmease.gitplex.search.hit.TextHit;
@@ -376,11 +377,7 @@ public abstract class SearchResultPanel extends Panel {
if (activeBlobIndex == blobItem.getIndex() && activeHitIndex == -1)
add(AttributeAppender.append("class", " active"));
- Long requestId;
- if (context.getPullRequest() != null)
- requestId = context.getPullRequest().getId();
- else
- requestId = null;
+ Long requestId = PullRequest.idOf(context.getPullRequest());
PageParameters params = RepoFilePage.paramsOf(
context.getRepository(), context.getBlobIdent().revision,
blobPath, null, requestId);
@@ -438,14 +435,11 @@ public abstract class SearchResultPanel extends Panel {
if (activeBlobIndex == blobItem.getIndex() && activeHitIndex == hitItem.getIndex())
add(AttributeAppender.append("class", " active"));
- Long requestId;
- if (context.getPullRequest() != null)
- requestId = context.getPullRequest().getId();
- else
- requestId = null;
+ Long requestId = PullRequest.idOf(context.getPullRequest());
+ Highlight highlight = Highlight.of(hit.getTokenPos());
PageParameters params = RepoFilePage.paramsOf(
context.getRepository(), context.getBlobIdent().revision,
- hit.getBlobPath(), new Highlight(hit.getTokenPos()), requestId);
+ hit.getBlobPath(), highlight, requestId);
CharSequence url = RequestCycle.get().urlFor(RepoFilePage.class, params);
add(AttributeAppender.replace("href", url.toString()));
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobview/source/source-view.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobview/source/source-view.js
index 3e42806c96..5670a93af3 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobview/source/source-view.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/blobview/source/source-view.js
@@ -158,7 +158,9 @@ gitplex.sourceview = {
});
if (commentId != -1) {
$comment = $("#pullrequest-comment-" + commentId);
- cm.setCursor($comment.data("lineno"));
+ var lineNo = $comment.data("lineno");
+ gitplex.sourceview.centerLine(cm, lineNo);
+ cm.setCursor(lineNo);
setTimeout(function() {$comment.find(">div").focus();}, 10);
}
@@ -169,7 +171,8 @@ gitplex.sourceview = {
document.getElementById(symbolTooltipId).onMouseOverSymbol(revision, node);
}
});
-
+ CodeMirror.keyMap.default["Ctrl-L"] = "gotoLine";
+
cm.focus();
}
if (cm.getOption("fullScreen"))
@@ -177,22 +180,26 @@ gitplex.sourceview = {
cm.setSize($code.width(), $code.height());
});
},
-
+
+ centerLine: function(cm, line) {
+ var h = cm.getScrollInfo().clientHeight;
+ var coords = cm.charCoords({line: line, ch: 0}, "local");
+ cm.scrollTo(null, (coords.top + coords.bottom - h) / 2);
+ },
+
highlight: function(cm, highlight) {
if (typeof cm === "string")
cm = $("#"+ cm + ">.CodeMirror")[0].CodeMirror;
if (highlight) {
- var h = cm.getScrollInfo().clientHeight;
- var coords = cm.charCoords({line: highlight.fromLine, ch: 0}, "local");
- cm.scrollTo(null, (coords.top + coords.bottom - h) / 2);
+ centerLine(cm, highlight.beginLine);
var allMarks = cm.getAllMarks();
for (var i=0; i<allMarks.length; i++)
allMarks[i].clear();
cm.markText(
- {line: highlight.fromLine, ch: highlight.fromChar},
- {line: highlight.toLine, ch: highlight.toChar},
+ {line: highlight.beginLine, ch: highlight.beginChar},
+ {line: highlight.endLine, ch: highlight.endChar},
{className: "CodeMirror-highlight"});
} else {
cm.setCursor(0, 0);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/fileedit/file-edit.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/fileedit/file-edit.js
index cbc85e66e1..ee8108fc42 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/fileedit/file-edit.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/fileedit/file-edit.js
@@ -40,23 +40,40 @@ gitplex.fileEdit = {
});
cm.focus();
+ // use scroll timer and cursor timer to minimize performance impact of
+ // remembering scroll and cursor position
+ var scrollTimer;
cm.on("scroll", function() {
- var scrollInfo = cm.getScrollInfo();
- pmease.commons.history.setScrollPos({left: scrollInfo.left, top: scrollInfo.top});
+ if (scrollTimer)
+ clearTimeout(scrollTimer);
+ scrollTimer = setTimeout(function() {
+ scrollTimer = undefined;
+ var scrollInfo = cm.getScrollInfo();
+ pmease.commons.history.setScrollPos({left: scrollInfo.left, top: scrollInfo.top});
+ }, 500);
});
var scrollPos = pmease.commons.history.getScrollPos();
if (scrollPos)
cm.scrollTo(scrollPos.left, scrollPos.top);
+ var cursorTimer;
cm.on("cursorActivity", function() {
- pmease.commons.history.setCursor(cm.getCursor());
+ if (cursorTimer)
+ clearTimeout(cursorTimer);
+ cursorTimer = setTimeout(function() {
+ cursorTimer = undefined;
+ pmease.commons.history.setCursor(cm.getCursor());
+ }, 500);
});
+
var cursor = pmease.commons.history.getCursor();
if (cursor)
cm.setCursor(cursor);
-
+
gitplex.fileEdit.setMode(cm, filePath);
+ CodeMirror.keyMap.default["Ctrl-L"] = "gotoLine";
+
$head.find("a.edit").click(function() {
gitplex.fileEdit.selectTab($(this));
});
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/symboltooltip/SymbolTooltipPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/symboltooltip/SymbolTooltipPanel.java
index 197fab54ed..51e78d688d 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/symboltooltip/SymbolTooltipPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/symboltooltip/SymbolTooltipPanel.java
@@ -218,22 +218,15 @@ public abstract class SymbolTooltipPanel extends Panel {
setOutputMarkupId(true);
}
- private Long getRequestId() {
- if (requestModel.getObject() != null)
- return requestModel.getObject().getId();
- else
- return null;
- }
-
public PageParameters getQueryHitParams(QueryHit hit) {
return RepoFilePage.paramsOf(
repoModel.getObject(), revision, hit.getBlobPath(),
- new Highlight(hit.getTokenPos()), getRequestId());
+ Highlight.of(hit.getTokenPos()), PullRequest.idOf(requestModel.getObject()));
}
public PageParameters getFindOccurrencesParams() {
return RepoFilePage.paramsOf(repoModel.getObject(), revision,
- getBlobPath(), null, symbol, null, getRequestId(), null);
+ getBlobPath(), null, symbol, null, PullRequest.idOf(requestModel.getObject()), null);
}
public String getSymbol() {
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/Highlight.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/Highlight.java
index 7840b1c223..d3b4b9369b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/Highlight.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/Highlight.java
@@ -2,6 +2,8 @@ package com.pmease.gitplex.web.page.repository.file;
import java.io.Serializable;
+import javax.annotation.Nullable;
+
import org.apache.commons.lang3.StringUtils;
import com.pmease.commons.lang.extractors.TokenPosition;
@@ -10,13 +12,13 @@ public class Highlight implements Serializable {
private static final long serialVersionUID = 1L;
- private final int fromLine, fromChar, toLine, toChar;
+ private final int beginLine, beginChar, endLine, endChar;
- public Highlight(int fromLine, int fromChar, int toLine, int toChar) {
- this.fromLine = fromLine;
- this.fromChar = fromChar;
- this.toLine = toLine;
- this.toChar = toChar;
+ public Highlight(int beginLine, int beginChar, int endLine, int endChar) {
+ this.beginLine = beginLine;
+ this.beginChar = beginChar;
+ this.endLine = endLine;
+ this.endChar = endChar;
}
public Highlight(TokenPosition tokenPos) {
@@ -25,33 +27,39 @@ public class Highlight implements Serializable {
}
public Highlight(String str) {
- String from = StringUtils.substringBefore(str, "-");
- String to = StringUtils.substringAfter(str, "-");
- fromLine = Integer.parseInt(StringUtils.substringBefore(from, ","))-1;
- fromChar = Integer.parseInt(StringUtils.substringAfter(from, ","))-1;
- toLine = Integer.parseInt(StringUtils.substringBefore(to, ","))-1;
- toChar = Integer.parseInt(StringUtils.substringAfter(to, ","))-1;
+ String begin = StringUtils.substringBefore(str, "-");
+ String end = StringUtils.substringAfter(str, "-");
+ beginLine = Integer.parseInt(StringUtils.substringBefore(begin, ","))-1;
+ beginChar = Integer.parseInt(StringUtils.substringAfter(begin, ","))-1;
+ endLine = Integer.parseInt(StringUtils.substringBefore(end, ","))-1;
+ endChar = Integer.parseInt(StringUtils.substringAfter(end, ","))-1;
}
- public int getFromLine() {
- return fromLine;
+ public int getBeginLine() {
+ return beginLine;
}
- public int getFromChar() {
- return fromChar;
+ public int getBeginChar() {
+ return beginChar;
}
- public int getToLine() {
- return toLine;
+ public int getEndLine() {
+ return endLine;
}
- public int getToChar() {
- return toChar;
+ public int getEndChar() {
+ return endChar;
}
@Override
public String toString() {
- return (fromLine+1) + "," + (fromChar+1) + "-" + (toLine+1) + "," + (toChar+1);
+ return (beginLine+1) + "," + (beginChar+1) + "-" + (endLine+1) + "," + (endChar+1);
}
+ public static @Nullable Highlight of(@Nullable TokenPosition tokenPos) {
+ if (tokenPos != null)
+ return new Highlight(tokenPos);
+ else
+ return null;
+ }
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
index d22de585ed..5d0fea7a03 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
@@ -491,34 +491,26 @@ public class RepoFilePage extends RepositoryPage implements BlobViewContext {
final AtomicReference<String> newPathRef = new AtomicReference<>(blobIdent.isTree()?null:blobIdent.path);
- IModel<Repository> repoModel = new LoadableDetachableModel<Repository>() {
-
- @Override
- protected Repository load() {
- return getEditRepository();
- }
-
- };
fileViewer = new FileEditPanel(
FILE_VIEWER_ID, repoModel, refName,
blobIdent.isTree()?null:blobIdent.path,
- blobIdent.isTree()?"":getEditRepository().getBlob(blobIdent).getText().getContent(),
+ blobIdent.isTree()?"":getRepository().getBlob(blobIdent).getText().getContent(),
getEditCommitId()) {
@Override
protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {
- Repository editRepository = getEditRepository();
- String editBranch = getEditBranch();
- editRepository.cacheObjectId(getEditBranch(), newCommitId);
+ Repository repository = getRepository();
+ String branch = blobIdent.revision;
+ repository.cacheObjectId(branch, newCommitId);
BlobIdent committed = new BlobIdent(
- editBranch, newPathRef.get(), FileMode.REGULAR_FILE.getBits());
+ branch, newPathRef.get(), FileMode.REGULAR_FILE.getBits());
for (RepositoryListener listener: GitPlex.getExtensions(RepositoryListener.class))
- listener.onRefUpdate(editRepository, refName, newCommitId.name());
+ listener.onRefUpdate(repository, refName, newCommitId.name());
- if (editRepository.equals(getRepository()))
+ if (repository.equals(getRepository()))
onSelect(target, committed, null);
else
- setResponsePage(RepoFilePage.class, paramsOf(editRepository, committed));
+ setResponsePage(RepoFilePage.class, paramsOf(repository, committed));
}
@Override
@@ -899,23 +891,15 @@ public class RepoFilePage extends RepositoryPage implements BlobViewContext {
};
- IModel<Repository> repoModel = new LoadableDetachableModel<Repository>() {
-
- @Override
- protected Repository load() {
- return getEditRepository();
- }
-
- };
fileViewer = new EditSavePanel(FILE_VIEWER_ID, repoModel, refName, blobIdent.path,
null, getEditCommitId(), cancelListener) {
@Override
protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {
- Repository editRepository = getEditRepository();
- String editBranch = getEditBranch();
- editRepository.cacheObjectId(editBranch, newCommitId);
- try ( FileRepository jgitRepo = editRepository.openAsJGitRepo();
+ Repository repository = getRepository();
+ String branch = blobIdent.revision;
+ repository.cacheObjectId(branch, newCommitId);
+ try ( FileRepository jgitRepo = repository.openAsJGitRepo();
RevWalk revWalk = new RevWalk(jgitRepo)) {
RevTree revTree = revWalk.parseCommit(newCommitId).getTree();
String parentPath = StringUtils.substringBeforeLast(blobIdent.path, "/");
@@ -928,12 +912,12 @@ public class RepoFilePage extends RepositoryPage implements BlobViewContext {
}
}
for (RepositoryListener listener: GitPlex.getExtensions(RepositoryListener.class))
- listener.onRefUpdate(editRepository, refName, newCommitId.name());
- BlobIdent parentBlobIdent = new BlobIdent(editBranch, parentPath, FileMode.TREE.getBits());
- if (editRepository.equals(getRepository()))
+ listener.onRefUpdate(repository, refName, newCommitId.name());
+ BlobIdent parentBlobIdent = new BlobIdent(branch, parentPath, FileMode.TREE.getBits());
+ if (repository.equals(getRepository()))
onSelect(target, parentBlobIdent, null);
else
- setResponsePage(RepoFilePage.class, paramsOf(editRepository, parentBlobIdent));
+ setResponsePage(RepoFilePage.class, paramsOf(repository, parentBlobIdent));
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -963,13 +947,6 @@ public class RepoFilePage extends RepositoryPage implements BlobViewContext {
return getRepository().getRefs(Git.REFS_HEADS).containsKey(blobIdent.revision);
}
- private Repository getEditRepository() {
- if (isOnBranch())
- return getRepository();
- else
- return requestModel.getObject().getSourceRepo();
- }
-
private String getEditRefName() {
if (isOnBranch())
return GitUtils.branch2ref(blobIdent.revision);
@@ -977,13 +954,6 @@ public class RepoFilePage extends RepositoryPage implements BlobViewContext {
return requestModel.getObject().getSourceRef();
}
- private String getEditBranch() {
- if (isOnBranch())
- return blobIdent.revision;
- else
- return requestModel.getObject().getSourceBranch();
- }
-
private ObjectId getEditCommitId() {
if (isOnBranch())
return getRepository().getObjectId(blobIdent.revision);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/repo-file.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/repo-file.css
index 3034f6f67e..4e639dfe1f 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/repo-file.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/repo-file.css
@@ -85,7 +85,7 @@
}
#repo-file>.last-commit {
padding: 6px 12px;
- background: #EEE;
+ background: #F2F2F2;
max-height: 600px;
overflow: auto;
}