summaryrefslogtreecommitdiff
path: root/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/SubstitutionShortInfoHandler.java
blob: ecdccbe0e40bac808c882ebb90b693ed5631ff04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.intellij.structuralsearch.plugin.ui;

import com.intellij.codeInsight.hint.TooltipController;
import com.intellij.codeInsight.template.impl.TemplateImplUtil;
import com.intellij.codeInsight.template.impl.Variable;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.event.*;
import com.intellij.openapi.util.Key;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Apr 23, 2004
 * Time: 5:20:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class SubstitutionShortInfoHandler implements DocumentListener, EditorMouseMotionListener, CaretListener {

  private long modificationTimeStamp;
  private final ArrayList<Variable> variables = new ArrayList<Variable>();
  private final Editor editor;
  public static final Key<Configuration> CURRENT_CONFIGURATION_KEY = Key.create("SS.CurrentConfiguration");

  SubstitutionShortInfoHandler(@NotNull Editor _editor) {
    editor = _editor;
  }

  public void beforeDocumentChange(DocumentEvent event) {
  }

  public void documentChanged(DocumentEvent event) {
  }

  public void mouseMoved(EditorMouseEvent e) {
    LogicalPosition position  = editor.xyToLogicalPosition( e.getMouseEvent().getPoint() );

    handleInputFocusMovement(position);
  }

  private void handleInputFocusMovement(LogicalPosition position) {
    checkModelValidity();
    String text = "";
    final int offset = editor.logicalPositionToOffset(position);
    final int length = editor.getDocument().getTextLength();
    final CharSequence elements = editor.getDocument().getCharsSequence();

    int start = offset-1;
    int end = -1;
    while(start >=0 && Character.isJavaIdentifierPart(elements.charAt(start)) && elements.charAt(start)!='$') start--;

    if (start >=0 && elements.charAt(start)=='$') {
      end = offset;

      while(end < length && Character.isJavaIdentifierPart(elements.charAt(end)) && elements.charAt(end)!='$') end++;
      if (end < length && elements.charAt(end)=='$') {
        String varname = elements.subSequence(start + 1, end).toString();
        Variable foundVar = null;

        for (final Variable var : variables) {
          if (var.getName().equals(varname)) {
            foundVar = var;
            break;
          }
        }

        if (foundVar!=null) {
          text = UIUtil.getShortParamString(editor.getUserData(CURRENT_CONFIGURATION_KEY),varname);
        }
      }
    }

    if (text.length() > 0) {
      UIUtil.showTooltip(editor, start, end + 1, text);
    }
    else {
      TooltipController.getInstance().cancelTooltips();
    }
  }

  private void checkModelValidity() {
    Document document = editor.getDocument();
    if (modificationTimeStamp != document.getModificationStamp()) {
      variables.clear();
      variables.addAll(TemplateImplUtil.parseVariables(document.getCharsSequence()).values());
      modificationTimeStamp = document.getModificationStamp();
    }
  }

  public void mouseDragged(EditorMouseEvent e) {
  }

  public void caretPositionChanged(CaretEvent e) {
    handleInputFocusMovement(e.getNewPosition());
  }

  @Override
  public void caretAdded(CaretEvent e) {
  }

  @Override
  public void caretRemoved(CaretEvent e) {
  }

  public ArrayList<Variable> getVariables() {
    checkModelValidity();
    return variables;
  }
}