Skip to content

Commit

Permalink
fix: #9 Add better text detection
Browse files Browse the repository at this point in the history
Ivan Scherbak committed Jan 14, 2016
1 parent 8d51afc commit 07f6c53
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -6,15 +6,20 @@
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.usages.Usage;
import com.intellij.usages.UsageInfo2UsageAdapter;
import com.intellij.usages.UsageView;
import com.intellij.usages.impl.UsageViewImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
@@ -43,6 +48,9 @@ public void actionPerformed(AnActionEvent e) {


StringBuilder buf = new StringBuilder();

Map<String, Boolean> processedLines = new HashMap<>();

for (Usage usage : usages) {

if (!(usage instanceof UsageInfo2UsageAdapter)) {
@@ -58,12 +66,29 @@ public void actionPerformed(AnActionEvent e) {

VirtualFile file = usageInfo.getFile();

int line = usageInfo.getLine();
String path = VfsUtil.getRelativePath(file, baseDir, '/');

String key = path + ":" + line;

if (processedLines.get(key) != null) {
continue;
}

Document fileDocument = FileDocumentManager.getInstance().getDocument(file);


int startOffset = fileDocument.getLineStartOffset(line);
int endOffset = fileDocument.getLineEndOffset(line);

String text = fileDocument.getText(new TextRange(startOffset, endOffset));

buf.append("\n");
buf.append("//file:" + path + ':' + (usageInfo.getLine() + 1) + "\n");
buf.append(usageInfo.getPlainText() + "\n");
buf.append("//file:" + path + ':' + (line + 1) + "\n");
buf.append(text + "\n");
buf.append("\n");

processedLines.put(key, true);
}


0 comments on commit 07f6c53

Please sign in to comment.