package cn.yiiguxing.plugin.translate.action;

import cn.yiiguxing.plugin.translate.Utils;
import cn.yiiguxing.plugin.translate.ui.Icons;
import cn.yiiguxing.plugin.translate.ui.TranslationBalloon;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DumbAware;

public class EditorPopupMenuAction extends AnAction implements DumbAware {

    public EditorPopupMenuAction() {
        super("Translate", "Translate", Icons.Translate);
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        Editor editor = getEditor(e);
        String selectedText = Utils.splitWord(getSelectedText(e));
        if (editor != null && !Utils.isEmptyOrBlankString(selectedText)) {
            new TranslationBalloon(editor).showAndQuery(selectedText);
        }
    }

    private Editor getEditor(AnActionEvent e) {
        return CommonDataKeys.EDITOR.getData(e.getDataContext());
    }

    @Override
    public void update(AnActionEvent e) {
        e.getPresentation().setEnabled(!Utils.isEmptyOrBlankString(getSelectedText(e)));
    }

    private String getSelectedText(AnActionEvent e) {
        Editor editor = getEditor(e);
        String selectedText = null;
        if (editor != null) {
            selectedText = editor.getSelectionModel().getSelectedText();
        }

        return selectedText;
    }

}

@@ -187,24 +187,86 @@
 ionBalloon;%0A
+import com.intellij.codeInsight.editorActions.SelectWordUtil;%0A
 import com.i
@@ -492,25 +492,213 @@
 api.
-project.DumbAware
+editor.SelectionModel;%0Aimport com.intellij.openapi.project.DumbAware;%0Aimport com.intellij.openapi.util.TextRange;%0Aimport com.sun.istack.internal.Nullable;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List
 ;%0A%0Ap
@@ -772,16 +772,70 @@
 ware %7B%0A%0A
+    @Nullable%0A    private TextRange mQueryTextRange;%0A%0A
     publ
@@ -1043,142 +1043,474 @@
     
-String selectedText = Utils.splitWord(getSelectedText(e));%0A        if (editor != null && !Utils.isEmptyOrBlankString(selectedText)) %7B%0A
+if (editor != null && hasQueryTextRange()) %7B%0A            String queryText = Utils.splitWord(editor.getDocument().getText(mQueryTextRange));%0A            if (!Utils.isEmptyOrBlankString(queryText)) %7B%0A                SelectionModel selectionModel = editor.getSelectionModel();%0A                if (!selectionModel.hasSelection()) %7B%0A                    selectionModel.setSelection(mQueryTextRange.getStartOffset(), mQueryTextRange.getEndOffset());%0A                %7D%0A%0A    
     
@@ -1565,22 +1565,33 @@
 ery(
-selectedText);
+queryText);%0A            %7D
 %0A   
@@ -1596,32 +1596,46 @@
        %7D%0A    %7D%0A%0A
+    @Nullable%0A
     private Edit
@@ -1747,145 +1747,294 @@
     
-@Override%0A    public void update(AnActionEvent e) %7B%0A        e.getPresentation().setEnabled(!Utils.isEmptyOrBlankString(getSelectedText(e)
+private boolean hasQueryTextRange() %7B%0A        return mQueryTextRange != null && !mQueryTextRange.isEmpty();%0A    %7D%0A%0A    @Override%0A    public void update(AnActionEvent e) %7B%0A        mQueryTextRange = getQueryTextRange(e);%0A        e.getPresentation().setEnabledAndVisible(hasQueryTextRange(
 ));%0A
@@ -2032,32 +2032,46 @@
 ange());%0A    %7D%0A%0A
+    @Nullable%0A
     private Stri
@@ -2070,30 +2070,35 @@
 ate 
-Stri
+TextRa
 ng
+e
  get
-SelectedText
+QueryTextRange
 (AnA
@@ -2125,72 +2125,286 @@
     
-Editor editor = getEditor(e);%0A        String selectedText = null
+TextRange queryRange = null;%0A%0A        Editor editor = getEditor(e);%0A        if (editor != null) %7B%0A            SelectionModel selectionModel = editor.getSelectionModel();%0A            if (!selectionModel.hasSelection()) %7B%0A                List%3CTextRange%3E ranges = new ArrayList%3C%3E()
 ;%0A  
@@ -2413,68 +2413,591 @@
     
+ 
-if (editor != null) %7B%0A            selectedText = editor.getS
+       SelectWordUtil.addWordOrLexemeSelection(false, editor, editor.getCaretModel().getOffset(), ranges);%0A%0A                if (!ranges.isEmpty()) %7B%0A                    TextRange maxRange = null;%0A                    for (TextRange range : ranges) %7B%0A                        if (maxRange == null %7C%7C range.contains(maxRange)) %7B%0A                            maxRange = range;%0A                        %7D%0A                    %7D%0A%0A                    queryRange = maxRange;%0A                %7D%0A            %7D else %7B%0A                queryRange = new TextRange(selectionModel.getSelectionStart(), s
 elec
@@ -3001,26 +3001,24 @@
 lectionModel
-()
 .getSelected
@@ -3019,17 +3019,32 @@
 lect
-edText();
+ionEnd());%0A            %7D
 %0A   
@@ -3070,20 +3070,18 @@
 urn 
-selectedText
+queryRange
 ;%0A  

优化取词：支持自动选词