Skip to content

Commit

Permalink
Merge branch '231.later'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonoas committed Jun 10, 2024
2 parents dc2fab7 + 96b5356 commit e48b754
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 80 deletions.
7 changes: 2 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ plugins {
}

group 'indi.bookmarkx'
version '1.1.1-SNAPSHOT'
version '2.0.0'
version '2.0.1'

repositories {
mavenLocal()
Expand All @@ -29,9 +28,7 @@ intellij {

patchPluginXml {
sinceBuild = '231.0'
changeNotes = "1. 分组支持添加描述<br>" +
"2. 分组支持重命名<br>" +
"3. 优化树节点样式显示<br>"
changeNotes = "1. 修复文本变动时,标签导航定位偏差问题<br>"
}

tasks.named('initializeIntelliJPlugin') {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/indi/bookmarkx/BookmarksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " ");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/indi/bookmarkx/RootWindowFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
94 changes: 30 additions & 64 deletions src/main/java/indi/bookmarkx/global/BookmarkDocumentListener.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<BookmarkNodeModel> onlyIndex = bookmarkArrayListTable.getOnlyIndex(virtualFile.getPath());
List<BookmarkNodeModel> 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<BookmarkNodeModel> list, LineRange lineRange, boolean isAdd) {
if (list == null || list.isEmpty()) {
private void perceivedLineChange(Project project, List<BookmarkNodeModel> indexList) {
if (CollectionUtils.isEmpty(indexList)) {
return;
}
BookmarkArrayListTable bookmarkArrayListTable = BookmarkArrayListTable.getInstance(project);
BookmarksManager bookmarksManager = BookmarksManager.getInstance(project);
List<BookmarkNodeModel> 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;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/indi/bookmarkx/model/BookmarkNodeModel.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/indi/bookmarkx/model/po/BookmarkPO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static BookmarkNodeModel findLine(List<BookmarkNodeModel> list, int i) {
return null;
}
Optional<BookmarkNodeModel> bookmarkNodeModel1 = list.stream()
.filter(bookmarkNodeModel -> bookmarkNodeModel.getOpenFileDescriptor().getLine() == i)
.filter(bookmarkNodeModel -> bookmarkNodeModel.getLine() == i)
.findFirst();
return bookmarkNodeModel1.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/indi/bookmarkx/ui/tree/BookmarkTree.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit e48b754

Please sign in to comment.