diff --git a/build.gradle b/build.gradle index 46e761e..ed8ae4c 100644 --- a/build.gradle +++ b/build.gradle @@ -4,8 +4,7 @@ plugins { } group 'indi.bookmarkx' -version '1.1.1-SNAPSHOT' -version '2.0.0' +version '2.0.1' repositories { mavenLocal() @@ -29,9 +28,7 @@ intellij { patchPluginXml { sinceBuild = '231.0' - changeNotes = "1. 分组支持添加描述
" + - "2. 分组支持重命名
" + - "3. 优化树节点样式显示
" + changeNotes = "1. 修复文本变动时,标签导航定位偏差问题
" } tasks.named('initializeIntelliJPlugin') { diff --git a/src/main/java/indi/bookmarkx/BookmarksManager.java b/src/main/java/indi/bookmarkx/BookmarksManager.java index 440ffd2..f6a1dd9 100644 --- a/src/main/java/indi/bookmarkx/BookmarksManager.java +++ b/src/main/java/indi/bookmarkx/BookmarksManager.java @@ -58,10 +58,9 @@ public void createBookRemark(Project project, Editor editor, VirtualFile file) { BookmarkNodeModel bookmarkNodeModel = LineEndPainter.findLine(BookmarkArrayListTable.getInstance(project).getOnlyIndex(file.getPath()), line); String defaultName = file.getName(); - String defaultDesc = null; + String defaultDesc; boolean add = true; if (bookmarkNodeModel == null) { - add = true; // 获取选中文本 String selectedText = caretModel.getCurrentCaret().getSelectedText(); defaultDesc = selectedText == null ? "" : (" " + selectedText + " "); diff --git a/src/main/java/indi/bookmarkx/RootWindowFactory.java b/src/main/java/indi/bookmarkx/RootWindowFactory.java index f8cf128..6217852 100644 --- a/src/main/java/indi/bookmarkx/RootWindowFactory.java +++ b/src/main/java/indi/bookmarkx/RootWindowFactory.java @@ -29,7 +29,7 @@ public void createToolWindowContent(@NotNull Project project, ToolWindow toolWin initManager(project, panel); - ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); + ContentFactory contentFactory = ContentFactory.getInstance(); Content regularRetention = contentFactory.createContent(panel, null, false); toolWindow.getContentManager().addContent(regularRetention); diff --git a/src/main/java/indi/bookmarkx/global/BookmarkDocumentListener.java b/src/main/java/indi/bookmarkx/global/BookmarkDocumentListener.java index c66cf9a..c0448bc 100644 --- a/src/main/java/indi/bookmarkx/global/BookmarkDocumentListener.java +++ b/src/main/java/indi/bookmarkx/global/BookmarkDocumentListener.java @@ -1,26 +1,25 @@ package indi.bookmarkx.global; -import com.intellij.diff.util.LineRange; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.editor.event.DocumentEvent; import com.intellij.openapi.editor.event.DocumentListener; import com.intellij.openapi.editor.impl.EditorFactoryImpl; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import indi.bookmarkx.BookmarksManager; import indi.bookmarkx.MyPersistent; import indi.bookmarkx.common.data.BookmarkArrayListTable; import indi.bookmarkx.model.BookmarkNodeModel; +import org.apache.commons.collections.CollectionUtils; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; /** * @author: codeleep @@ -32,99 +31,66 @@ public class BookmarkDocumentListener implements DocumentListener { private static final Logger LOG = Logger.getInstance(MyPersistent.class); @Override - public void beforeDocumentChange(@NotNull DocumentEvent event) { + public void documentChanged(@NotNull DocumentEvent event) { try { Document document = event.getDocument(); - CharSequence newFragment = event.getNewFragment(); - CharSequence oldFragment = event.getOldFragment(); - // 获取行变化 - String newStr = String.valueOf(newFragment); - String oldStr = String.valueOf(oldFragment); - int newLineCount = StringUtil.countChars(newStr, '\n'); - int oldLineCount = StringUtil.countChars(oldStr, '\n'); - if ((newLineCount <= 0 && oldLineCount <= 0) || newLineCount == oldLineCount){ - return; - } + VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document); - if (virtualFile == null) { - return; - } Editor editor = getEditor(document); - if (editor == null) { + + if (virtualFile == null || editor == null) { return; } + Project project = editor.getProject(); if (project == null) { return; } BookmarkArrayListTable bookmarkArrayListTable = BookmarkArrayListTable.getInstance(project); - List onlyIndex = bookmarkArrayListTable.getOnlyIndex(virtualFile.getPath()); + List indexList = bookmarkArrayListTable.getOnlyIndex(virtualFile.getPath()); // 空的直接返回 - if (onlyIndex == null || onlyIndex.isEmpty()) { + if (CollectionUtils.isEmpty(indexList)) { return; } - // 计算行变化 - int offset = event.getOffset(); - // 变化所在行 - int startLineNumber = 0; - int endLineNumber = 0; - boolean isAdd = true; - if (newLineCount > oldLineCount) { - isAdd = true; - startLineNumber = document.getLineNumber(offset) + 1; - endLineNumber = startLineNumber + newLineCount - oldLineCount; - }else { - isAdd = false; - endLineNumber = document.getLineNumber(offset) + 1; - startLineNumber = endLineNumber - oldLineCount + newLineCount; - } - LineRange lineRange = new LineRange(startLineNumber, endLineNumber); - perceivedLineChange(project, virtualFile, onlyIndex, lineRange, isAdd); - }catch (Exception e) { + + perceivedLineChange(project, indexList); + } catch (Exception e) { LOG.info("perceivedLineChange error", e); } } - private void perceivedLineChange(Project project, VirtualFile virtualFile, List list, LineRange lineRange, boolean isAdd) { - if (list == null || list.isEmpty()) { + private void perceivedLineChange(Project project, List indexList) { + if (CollectionUtils.isEmpty(indexList)) { return; } BookmarkArrayListTable bookmarkArrayListTable = BookmarkArrayListTable.getInstance(project); BookmarksManager bookmarksManager = BookmarksManager.getInstance(project); List removeList = new ArrayList<>(); - for (BookmarkNodeModel node : list) { - if (node == null){ + Document document; + for (BookmarkNodeModel node : indexList) { + if (node == null) { continue; } - int positionLine = Integer.parseInt(String.valueOf(node.getLine())); - int rowGap = lineRange.end - lineRange.start; - int changeLine = lineRange.start; - if(isAdd) { - if (positionLine + 1 < changeLine) { - continue; - } - node.setLine(positionLine + rowGap); - node.setOpenFileDescriptor(new OpenFileDescriptor(project, virtualFile, positionLine + rowGap, 0)); - }else { - if (positionLine <= changeLine) { - continue; - } - if (positionLine < lineRange.end && positionLine > lineRange.start) { - removeList.add(node); - continue; - } - node.setLine(positionLine - rowGap); - node.setOpenFileDescriptor(new OpenFileDescriptor(project, virtualFile, positionLine - rowGap, 0)); + OpenFileDescriptor descriptor = node.getOpenFileDescriptor(); + RangeMarker rangeMarker = descriptor.getRangeMarker(); + + if (null == rangeMarker || !rangeMarker.isValid()) { + // 移除行尾描述信息 + removeList.add(node); + } else { + document = rangeMarker.getDocument(); + int line = document.getLineNumber(rangeMarker.getStartOffset()); + node.setLine(line); } - removeList.forEach(bookmarkArrayListTable::delete); - bookmarksManager.persistentSave(); } + removeList.forEach(bookmarkArrayListTable::delete); + bookmarksManager.persistentSave(); } private Editor getEditor(Document document) { Editor[] editors = EditorFactoryImpl.getInstance().getEditors(document); - if (editors.length >= 1){ + if (editors.length >= 1) { return editors[0]; } return null; diff --git a/src/main/java/indi/bookmarkx/model/BookmarkNodeModel.java b/src/main/java/indi/bookmarkx/model/BookmarkNodeModel.java index 200aa85..5ec634c 100644 --- a/src/main/java/indi/bookmarkx/model/BookmarkNodeModel.java +++ b/src/main/java/indi/bookmarkx/model/BookmarkNodeModel.java @@ -1,6 +1,10 @@ package indi.bookmarkx.model; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.fileEditor.OpenFileDescriptor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import javax.swing.Icon; import java.util.Objects; @@ -62,8 +66,13 @@ public int getLine() { return line; } - public void setLine(int line) { - this.line = line; + /** + * 设置当前值时,会同步更新 {@link BookmarkNodeModel#openFileDescriptor} + * + * @param newLine 新值 + */ + public void setLine(int newLine) { + this.line = newLine; } public int getColumn() { diff --git a/src/main/java/indi/bookmarkx/model/po/BookmarkPO.java b/src/main/java/indi/bookmarkx/model/po/BookmarkPO.java index c085077..04eff48 100644 --- a/src/main/java/indi/bookmarkx/model/po/BookmarkPO.java +++ b/src/main/java/indi/bookmarkx/model/po/BookmarkPO.java @@ -2,7 +2,6 @@ import javax.xml.bind.annotation.XmlRootElement; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/indi/bookmarkx/ui/painter/LineEndPainter.java b/src/main/java/indi/bookmarkx/ui/painter/LineEndPainter.java index 6fbc1dc..d76f510 100644 --- a/src/main/java/indi/bookmarkx/ui/painter/LineEndPainter.java +++ b/src/main/java/indi/bookmarkx/ui/painter/LineEndPainter.java @@ -57,7 +57,7 @@ public static BookmarkNodeModel findLine(List list, int i) { return null; } Optional bookmarkNodeModel1 = list.stream() - .filter(bookmarkNodeModel -> bookmarkNodeModel.getOpenFileDescriptor().getLine() == i) + .filter(bookmarkNodeModel -> bookmarkNodeModel.getLine() == i) .findFirst(); return bookmarkNodeModel1.orElse(null); } diff --git a/src/main/java/indi/bookmarkx/ui/pannel/BookmarkTipPanel.java b/src/main/java/indi/bookmarkx/ui/pannel/BookmarkTipPanel.java index 70fdaac..58c7d84 100644 --- a/src/main/java/indi/bookmarkx/ui/pannel/BookmarkTipPanel.java +++ b/src/main/java/indi/bookmarkx/ui/pannel/BookmarkTipPanel.java @@ -6,7 +6,7 @@ import com.intellij.util.ui.JBUI; import indi.bookmarkx.model.AbstractTreeNodeModel; import indi.bookmarkx.utils.HtmlUtil; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/indi/bookmarkx/ui/tree/BookmarkTree.java b/src/main/java/indi/bookmarkx/ui/tree/BookmarkTree.java index a5cd5b0..49d55be 100644 --- a/src/main/java/indi/bookmarkx/ui/tree/BookmarkTree.java +++ b/src/main/java/indi/bookmarkx/ui/tree/BookmarkTree.java @@ -1,7 +1,7 @@ package indi.bookmarkx.ui.tree; +import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.InputValidatorEx; import com.intellij.openapi.ui.JBMenuItem; import com.intellij.openapi.ui.JBPopupMenu; import com.intellij.openapi.ui.Messages; @@ -18,7 +18,6 @@ import indi.bookmarkx.ui.dialog.BookmarkCreatorDialog; import indi.bookmarkx.ui.pannel.BookmarkTipPanel; import org.apache.commons.lang3.Validate; -import org.jsoup.internal.StringUtil; import javax.swing.DropMode; import javax.swing.JComponent; @@ -190,7 +189,9 @@ public void mouseClicked(MouseEvent e) { BookmarkTreeNode selectedNode = (BookmarkTreeNode) path.getLastPathComponent(); if (selectedNode != null && selectedNode.isBookmark()) { BookmarkNodeModel bookmark = (BookmarkNodeModel) selectedNode.getUserObject(); - bookmark.getOpenFileDescriptor().navigate(true); + + OpenFileDescriptor fileDescriptor = bookmark.getOpenFileDescriptor(); + fileDescriptor.navigate(true); } }