author | wenzelm |
Fri, 20 Jan 2023 20:26:42 +0100 | |
changeset 77029 | 1046a69fabaa |
parent 76794 | 941d4f527091 |
child 80225 | d9ff4296e3b7 |
permissions | -rw-r--r-- |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Tools/jEdit/src/completion_popup.scala |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
3 |
|
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
4 |
Completion popup. |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
5 |
*/ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
6 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
7 |
package isabelle.jedit |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
8 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
9 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
10 |
import isabelle._ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
11 |
|
53297 | 12 |
import java.awt.{Color, Font, Point, BorderLayout, Dimension} |
71601 | 13 |
import java.awt.event.{KeyEvent, KeyListener, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent} |
56843
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56842
diff
changeset
|
14 |
import java.io.{File => JFile} |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
15 |
import javax.swing.{JPanel, JComponent, JLayeredPane, SwingUtilities} |
53297 | 16 |
import javax.swing.border.LineBorder |
53848
8d7029eb0c31
disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents:
53784
diff
changeset
|
17 |
import javax.swing.text.DefaultCaret |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
18 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
19 |
import scala.swing.{ListView, ScrollPane} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
20 |
import scala.swing.event.MouseClicked |
53784 | 21 |
import org.gjt.sp.jedit.View |
56325
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
22 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, Selection} |
53784 | 23 |
import org.gjt.sp.jedit.gui.{HistoryTextField, KeyEventWorkaround} |
56843
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56842
diff
changeset
|
24 |
import org.gjt.sp.util.StandardUtilities |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
25 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
26 |
|
75393 | 27 |
object Completion_Popup { |
55978 | 28 |
/** items with HTML rendering **/ |
29 |
||
75393 | 30 |
private class Item(val item: Completion.Item) { |
55978 | 31 |
private val html = |
32 |
item.description match { |
|
33 |
case a :: bs => |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62113
diff
changeset
|
34 |
"<html><b>" + HTML.output(Symbol.print_newlines(a)) + "</b>" + |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62113
diff
changeset
|
35 |
HTML.output(bs.map(b => " " + Symbol.print_newlines(b)).mkString) + "</html>" |
55978 | 36 |
case Nil => "" |
37 |
} |
|
38 |
override def toString: String = html |
|
39 |
} |
|
40 |
||
41 |
||
42 |
||
53784 | 43 |
/** jEdit text area **/ |
53235
1b6a44859549
register single instance within root, in order to get rid of old popup as user continues typing;
wenzelm
parents:
53234
diff
changeset
|
44 |
|
75393 | 45 |
object Text_Area { |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
46 |
private val key = new Object |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
47 |
|
75393 | 48 |
def apply(text_area: TextArea): Option[Completion_Popup.Text_Area] = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
49 |
GUI_Thread.require {} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
50 |
text_area.getClientProperty(key) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
51 |
case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
52 |
case _ => None |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
53 |
} |
55712 | 54 |
} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
55 |
|
55712 | 56 |
def active_range(text_area: TextArea): Option[Text.Range] = |
57 |
apply(text_area) match { |
|
58 |
case Some(text_area_completion) => text_area_completion.active_range |
|
59 |
case None => None |
|
55711 | 60 |
} |
61 |
||
56586 | 62 |
def action(text_area: TextArea, word_only: Boolean): Boolean = |
56170
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
63 |
apply(text_area) match { |
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
64 |
case Some(text_area_completion) => |
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
65 |
if (text_area_completion.active_range.isDefined) |
56586 | 66 |
text_area_completion.action(word_only = word_only) |
56177
bfa9dfb722de
proper flags for main action (amending 638b29331549);
wenzelm
parents:
56175
diff
changeset
|
67 |
else |
56586 | 68 |
text_area_completion.action(immediate = true, explicit = true, word_only = word_only) |
56170
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
69 |
true |
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
70 |
case None => false |
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
71 |
} |
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
72 |
|
75393 | 73 |
def exit(text_area: JEditTextArea): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
74 |
GUI_Thread.require {} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
75 |
apply(text_area) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
76 |
case None => |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
77 |
case Some(text_area_completion) => |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
78 |
text_area_completion.deactivate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
79 |
text_area.putClientProperty(key, null) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
80 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
81 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
82 |
|
75393 | 83 |
def init(text_area: JEditTextArea): Completion_Popup.Text_Area = { |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
84 |
exit(text_area) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
85 |
val text_area_completion = new Text_Area(text_area) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
86 |
text_area.putClientProperty(key, text_area_completion) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
87 |
text_area_completion.activate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
88 |
text_area_completion |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
89 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
90 |
|
75393 | 91 |
def dismissed(text_area: TextArea): Boolean = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
92 |
GUI_Thread.require {} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
93 |
apply(text_area) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
94 |
case Some(text_area_completion) => text_area_completion.dismissed() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
95 |
case None => false |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
96 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
97 |
} |
53272 | 98 |
} |
99 |
||
75393 | 100 |
class Text_Area private(text_area: JEditTextArea) { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
101 |
// owned by GUI thread |
53272 | 102 |
private var completion_popup: Option[Completion_Popup] = None |
103 |
||
55711 | 104 |
def active_range: Option[Text.Range] = |
105 |
completion_popup match { |
|
56605 | 106 |
case Some(completion) => completion.active_range |
55711 | 107 |
case None => None |
108 |
} |
|
109 |
||
53272 | 110 |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
111 |
/* rendering */ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
112 |
|
75393 | 113 |
def rendering(rendering: JEdit_Rendering, line_range: Text.Range): Option[Text.Info[Color]] = { |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
114 |
active_range match { |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
115 |
case Some(range) => range.try_restrict(line_range) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
116 |
case None => |
66117 | 117 |
val caret = text_area.getCaretPosition |
118 |
if (line_range.contains(caret)) { |
|
119 |
rendering.before_caret_range(caret).try_restrict(line_range) match { |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
120 |
case Some(range) if !range.is_singularity => |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
121 |
val range0 = |
66157 | 122 |
Completion.Result.merge(Completion.History.empty, |
61734 | 123 |
syntax_completion(Completion.History.empty, true, Some(rendering)), |
77029
1046a69fabaa
dismantle special treatment of citations in Isabelle/Scala;
wenzelm
parents:
76794
diff
changeset
|
124 |
rendering.path_completion(caret)) |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
125 |
.map(_.range) |
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
126 |
rendering.semantic_completion(range0, range) match { |
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
127 |
case None => range0 |
56175 | 128 |
case Some(Text.Info(_, Completion.No_Completion)) => None |
129 |
case Some(Text.Info(range1, _: Completion.Names)) => Some(range1) |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
130 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
131 |
case _ => None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
132 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
133 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
134 |
else None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
135 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
136 |
}.map(range => Text.Info(range, rendering.completion_color)) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
137 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
138 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
139 |
/* syntax completion */ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
140 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
141 |
def syntax_completion( |
56175 | 142 |
history: Completion.History, |
143 |
explicit: Boolean, |
|
75393 | 144 |
opt_rendering: Option[JEdit_Rendering] |
145 |
): Option[Completion.Result] = { |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
146 |
val buffer = text_area.getBuffer |
66055 | 147 |
val unicode = Isabelle_Encoding.is_active(buffer) |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
148 |
|
59074 | 149 |
Isabelle.buffer_syntax(buffer) match { |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
150 |
case Some(syntax) => |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
151 |
val context = |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56586
diff
changeset
|
152 |
(for { |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56586
diff
changeset
|
153 |
rendering <- opt_rendering |
56842
b6e266574b26
yet another completion option, to imitate old less ambitious behavior;
wenzelm
parents:
56841
diff
changeset
|
154 |
if PIDE.options.bool("jedit_completion_context") |
66117 | 155 |
caret_range = rendering.before_caret_range(text_area.getCaretPosition) |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65999
diff
changeset
|
156 |
context <- rendering.language_context(caret_range) |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56586
diff
changeset
|
157 |
} yield context) getOrElse syntax.language_context |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
158 |
|
56589
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
159 |
val caret = text_area.getCaretPosition |
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
160 |
val line_range = JEdit_Lib.line_range(buffer, text_area.getCaretLine) |
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
161 |
val line_start = line_range.start |
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
162 |
for { |
67014 | 163 |
line_text <- JEdit_Lib.get_text(buffer, line_range) |
56589
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
164 |
result <- |
67005 | 165 |
syntax.complete( |
66055 | 166 |
history, unicode, explicit, line_start, line_text, caret - line_start, context) |
56589
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
167 |
} yield result |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
168 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
169 |
case None => None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
170 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
171 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
172 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
173 |
|
56170
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
174 |
/* completion action: text area */ |
53273 | 175 |
|
75393 | 176 |
private def insert(item: Completion.Item): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
177 |
GUI_Thread.require {} |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
178 |
|
53242 | 179 |
val buffer = text_area.getBuffer |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
180 |
val range = item.range |
56863 | 181 |
if (buffer.isEditable) { |
53242 | 182 |
JEdit_Lib.buffer_edit(buffer) { |
67014 | 183 |
JEdit_Lib.get_text(buffer, range) match { |
53242 | 184 |
case Some(text) if text == item.original => |
56325
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
185 |
text_area.getSelectionAtOffset(text_area.getCaretPosition) match { |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
186 |
|
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
187 |
/*rectangular selection as "tall caret"*/ |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
188 |
case selection : Selection.Rect |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
189 |
if selection.getStart(buffer, text_area.getCaretLine) == range.stop => |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
190 |
text_area.moveCaretPosition(range.stop) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
191 |
(0 until Character.codePointCount(item.original, 0, item.original.length)) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
192 |
.foreach(_ => text_area.backspace()) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
193 |
text_area.setSelectedText(selection, item.replacement) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
194 |
text_area.moveCaretPosition(text_area.getCaretPosition + item.move) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
195 |
|
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
196 |
/*other selections: rectangular, multiple range, ...*/ |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
197 |
case selection |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
198 |
if selection != null && |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
199 |
selection.getStart(buffer, text_area.getCaretLine) == range.start && |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
200 |
selection.getEnd(buffer, text_area.getCaretLine) == range.stop => |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
201 |
text_area.moveCaretPosition(range.stop + item.move) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
202 |
text_area.getSelection.foreach(text_area.setSelectedText(_, item.replacement)) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
203 |
|
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
204 |
/*no selection*/ |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
205 |
case _ => |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
206 |
buffer.remove(range.start, range.length) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
207 |
buffer.insert(range.start, item.replacement) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
208 |
text_area.moveCaretPosition(range.start + item.replacement.length + item.move) |
66180 | 209 |
Isabelle.indent_input(text_area) |
56325
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
210 |
} |
53242 | 211 |
case _ => |
212 |
} |
|
53233 | 213 |
} |
214 |
} |
|
215 |
} |
|
53242 | 216 |
|
56586 | 217 |
def action( |
218 |
immediate: Boolean = false, |
|
219 |
explicit: Boolean = false, |
|
220 |
delayed: Boolean = false, |
|
75393 | 221 |
word_only: Boolean = false |
222 |
): Boolean = { |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
223 |
val view = text_area.getView |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
224 |
val layered = view.getLayeredPane |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
225 |
val buffer = text_area.getBuffer |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
226 |
val painter = text_area.getPainter |
53273 | 227 |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
228 |
val history = PIDE.plugin.completion_history.value |
66055 | 229 |
val unicode = Isabelle_Encoding.is_active(buffer) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
230 |
|
75393 | 231 |
def open_popup(result: Completion.Result): Unit = { |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
232 |
val font = |
55825 | 233 |
painter.getFont.deriveFont( |
234 |
Font_Info.main_size(PIDE.options.real("jedit_popup_font_scale"))) |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
235 |
|
55711 | 236 |
val range = result.range |
237 |
||
238 |
val loc1 = text_area.offsetToXY(range.start) |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
239 |
if (loc1 != null) { |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
240 |
val loc2 = |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
241 |
SwingUtilities.convertPoint(painter, |
58767
30766b5fd0e1
proper line height and text base line, like regular TextAreaPainter.PaintText;
wenzelm
parents:
58592
diff
changeset
|
242 |
loc1.x, loc1.y + painter.getLineHeight, layered) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
243 |
|
55978 | 244 |
val items = result.items.map(new Item(_)) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
245 |
val completion = |
75393 | 246 |
new Completion_Popup(Some(range), layered, loc2, font, items) { |
247 |
override def complete(item: Completion.Item): Unit = { |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
248 |
PIDE.plugin.completion_history.update(item) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
249 |
insert(item) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
250 |
} |
75393 | 251 |
override def propagate(evt: KeyEvent): Unit = { |
56840
879fe16bd27c
propagate more events, notably after hide_popup (e.g. LEFT, RIGHT);
wenzelm
parents:
56662
diff
changeset
|
252 |
if (view.getKeyEventInterceptor == null) |
879fe16bd27c
propagate more events, notably after hide_popup (e.g. LEFT, RIGHT);
wenzelm
parents:
56662
diff
changeset
|
253 |
JEdit_Lib.propagate_key(view, evt) |
879fe16bd27c
propagate more events, notably after hide_popup (e.g. LEFT, RIGHT);
wenzelm
parents:
56662
diff
changeset
|
254 |
else if (view.getKeyEventInterceptor == inner_key_listener) { |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
255 |
try { |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
256 |
view.setKeyEventInterceptor(null) |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
257 |
JEdit_Lib.propagate_key(view, evt) |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
258 |
} |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
259 |
finally { |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
260 |
if (isDisplayable) view.setKeyEventInterceptor(inner_key_listener) |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
261 |
} |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
262 |
} |
56171
15351577da10
clarified key event propagation, in accordance to outer_key_listener;
wenzelm
parents:
56170
diff
changeset
|
263 |
if (evt.getID == KeyEvent.KEY_TYPED) input(evt) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
264 |
} |
75393 | 265 |
override def shutdown(focus: Boolean): Unit = { |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
266 |
if (view.getKeyEventInterceptor == inner_key_listener) |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
267 |
view.setKeyEventInterceptor(null) |
73367 | 268 |
if (focus) text_area.requestFocus() |
56606
68b7a6db4a32
avoid ooddity: invoke intended function instead of java.awt.Container.invalidate();
wenzelm
parents:
56605
diff
changeset
|
269 |
JEdit_Lib.invalidate_range(text_area, range) |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
270 |
} |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
271 |
} |
57127
a406e15c3cf7
make double-sure that old popup is dismissed, before replacing it;
wenzelm
parents:
57051
diff
changeset
|
272 |
dismissed() |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
273 |
completion_popup = Some(completion) |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
274 |
view.setKeyEventInterceptor(completion.inner_key_listener) |
56606
68b7a6db4a32
avoid ooddity: invoke intended function instead of java.awt.Container.invalidate();
wenzelm
parents:
56605
diff
changeset
|
275 |
JEdit_Lib.invalidate_range(text_area, range) |
56841 | 276 |
Pretty_Tooltip.dismissed_all() |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
277 |
completion.show_popup(false) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
278 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
279 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
280 |
|
56175 | 281 |
if (buffer.isEditable) { |
66117 | 282 |
val caret = text_area.getCaretPosition |
76765
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents:
75807
diff
changeset
|
283 |
val opt_rendering = Document_View.get_rendering(text_area) |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
284 |
val result0 = syntax_completion(history, explicit, opt_rendering) |
75393 | 285 |
val (no_completion, semantic_completion) = { |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
286 |
opt_rendering match { |
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
287 |
case Some(rendering) => |
66055 | 288 |
rendering.semantic_completion_result(history, unicode, result0.map(_.range), |
66117 | 289 |
rendering.before_caret_range(caret)) |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
290 |
case None => (false, None) |
56175 | 291 |
} |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
292 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
293 |
if (no_completion) false |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
294 |
else { |
75393 | 295 |
val result = { |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
296 |
val result1 = |
56586 | 297 |
if (word_only) None |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
298 |
else Completion.Result.merge(history, semantic_completion, result0) |
56564
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56326
diff
changeset
|
299 |
opt_rendering match { |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
300 |
case None => result1 |
56564
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56326
diff
changeset
|
301 |
case Some(rendering) => |
66157 | 302 |
Completion.Result.merge(history, |
66151 | 303 |
result1, |
304 |
JEdit_Spell_Checker.completion(rendering, explicit, caret), |
|
77029
1046a69fabaa
dismantle special treatment of citations in Isabelle/Scala;
wenzelm
parents:
76794
diff
changeset
|
305 |
rendering.path_completion(caret)) |
56564
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56326
diff
changeset
|
306 |
} |
56586 | 307 |
} |
56175 | 308 |
result match { |
309 |
case Some(result) => |
|
310 |
result.items match { |
|
311 |
case List(item) if result.unique && item.immediate && immediate => |
|
312 |
insert(item) |
|
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
313 |
true |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
314 |
case _ :: _ if !delayed => |
56175 | 315 |
open_popup(result) |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
316 |
false |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
317 |
case _ => false |
56175 | 318 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
319 |
case None => false |
56175 | 320 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
321 |
} |
55752 | 322 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
323 |
else false |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
324 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
325 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
326 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
327 |
/* input key events */ |
53273 | 328 |
|
75393 | 329 |
def input(evt: KeyEvent): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
330 |
GUI_Thread.require {} |
53233 | 331 |
|
66180 | 332 |
if (!evt.isConsumed) { |
333 |
val special = JEdit_Lib.special_key(evt) |
|
334 |
||
335 |
if (PIDE.options.bool("jedit_completion")) { |
|
53273 | 336 |
dismissed() |
66185 | 337 |
if (evt.getKeyChar != '\b') { |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
338 |
val immediate = PIDE.options.bool("jedit_completion_immediate") |
53397
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
339 |
if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) { |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
340 |
input_delay.revoke() |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
341 |
action(immediate = immediate) |
53397
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
342 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
343 |
else { |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
344 |
if (!special && action(immediate = immediate, delayed = true)) |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
345 |
input_delay.revoke() |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
346 |
else |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
347 |
input_delay.invoke() |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
348 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
349 |
} |
53242 | 350 |
} |
66180 | 351 |
|
66186 | 352 |
val selection = text_area.getSelection() |
71383 | 353 |
if (!special && (selection == null || selection.isEmpty)) |
66186 | 354 |
Isabelle.indent_input(text_area) |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
355 |
} |
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
356 |
} |
53273 | 357 |
|
358 |
private val input_delay = |
|
71704 | 359 |
Delay.last(PIDE.options.seconds("jedit_completion_delay"), gui = true) { |
53322 | 360 |
action() |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
361 |
} |
53273 | 362 |
|
363 |
||
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
364 |
/* dismiss popup */ |
53273 | 365 |
|
75393 | 366 |
def dismissed(): Boolean = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
367 |
GUI_Thread.require {} |
53273 | 368 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
369 |
completion_popup match { |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
370 |
case Some(completion) => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
371 |
completion.hide_popup() |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
372 |
completion_popup = None |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
373 |
true |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
374 |
case None => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
375 |
false |
53273 | 376 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
377 |
} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
378 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
379 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
380 |
/* activation */ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
381 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
382 |
private val outer_key_listener = |
71601 | 383 |
JEdit_Lib.key_listener(key_typed = input) |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
384 |
|
73340 | 385 |
private def activate(): Unit = |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
386 |
text_area.addKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
387 |
|
75393 | 388 |
private def deactivate(): Unit = { |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
389 |
dismissed() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
390 |
text_area.removeKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
391 |
} |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
392 |
} |
53784 | 393 |
|
394 |
||
395 |
||
396 |
/** history text field **/ |
|
397 |
||
398 |
class History_Text_Field( |
|
399 |
name: String = null, |
|
400 |
instant_popups: Boolean = false, |
|
401 |
enter_adds_to_history: Boolean = true, |
|
67004
af72fa58f71b
clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents:
66186
diff
changeset
|
402 |
syntax: Outer_Syntax = Outer_Syntax.empty) extends |
75393 | 403 |
HistoryTextField(name, instant_popups, enter_adds_to_history |
404 |
) { |
|
53784 | 405 |
text_field => |
406 |
||
53848
8d7029eb0c31
disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents:
53784
diff
changeset
|
407 |
// see https://forums.oracle.com/thread/1361677 |
8d7029eb0c31
disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents:
53784
diff
changeset
|
408 |
if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret) |
53784 | 409 |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
410 |
// owned by GUI thread |
53784 | 411 |
private var completion_popup: Option[Completion_Popup] = None |
412 |
||
413 |
||
414 |
/* dismiss */ |
|
415 |
||
75393 | 416 |
private def dismissed(): Boolean = { |
53784 | 417 |
completion_popup match { |
418 |
case Some(completion) => |
|
419 |
completion.hide_popup() |
|
420 |
completion_popup = None |
|
421 |
true |
|
422 |
case None => |
|
423 |
false |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
||
428 |
/* insert */ |
|
429 |
||
75393 | 430 |
private def insert(item: Completion.Item): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
431 |
GUI_Thread.require {} |
53784 | 432 |
|
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
433 |
val range = item.range |
56863 | 434 |
if (text_field.isEditable) { |
53784 | 435 |
val content = text_field.getText |
66114 | 436 |
range.try_substring(content) match { |
53784 | 437 |
case Some(text) if text == item.original => |
438 |
text_field.setText( |
|
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
439 |
content.substring(0, range.start) + |
53784 | 440 |
item.replacement + |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
441 |
content.substring(range.stop)) |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
442 |
text_field.getCaret.setDot(range.start + item.replacement.length + item.move) |
53784 | 443 |
case _ => |
444 |
} |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
||
56170
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
449 |
/* completion action: text field */ |
53784 | 450 |
|
75393 | 451 |
def action(): Unit = { |
53784 | 452 |
GUI.layered_pane(text_field) match { |
453 |
case Some(layered) if text_field.isEditable => |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
454 |
val history = PIDE.plugin.completion_history.value |
53784 | 455 |
|
456 |
val caret = text_field.getCaret.getDot |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
457 |
val text = text_field.getText |
55748 | 458 |
|
55749 | 459 |
val context = syntax.language_context |
55748 | 460 |
|
67005 | 461 |
syntax.complete(history, true, false, 0, text, caret, context) match { |
53784 | 462 |
case Some(result) => |
463 |
val fm = text_field.getFontMetrics(text_field.getFont) |
|
464 |
val loc = |
|
465 |
SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered) |
|
466 |
||
55978 | 467 |
val items = result.items.map(new Item(_)) |
55711 | 468 |
val completion = |
75393 | 469 |
new Completion_Popup(None, layered, loc, text_field.getFont, items) { |
470 |
override def complete(item: Completion.Item): Unit = { |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
471 |
PIDE.plugin.completion_history.update(item) |
55978 | 472 |
insert(item) |
473 |
} |
|
73340 | 474 |
override def propagate(evt: KeyEvent): Unit = |
55978 | 475 |
if (!evt.isConsumed) text_field.processKeyEvent(evt) |
73340 | 476 |
override def shutdown(focus: Boolean): Unit = |
73367 | 477 |
if (focus) text_field.requestFocus() |
53784 | 478 |
} |
57127
a406e15c3cf7
make double-sure that old popup is dismissed, before replacing it;
wenzelm
parents:
57051
diff
changeset
|
479 |
dismissed() |
53784 | 480 |
completion_popup = Some(completion) |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
481 |
completion.show_popup(true) |
53784 | 482 |
|
483 |
case None => |
|
484 |
} |
|
485 |
case _ => |
|
486 |
} |
|
487 |
} |
|
488 |
||
489 |
||
490 |
/* process key event */ |
|
491 |
||
75393 | 492 |
private def process(evt: KeyEvent): Unit = { |
53784 | 493 |
if (PIDE.options.bool("jedit_completion")) { |
494 |
dismissed() |
|
495 |
if (evt.getKeyChar != '\b') { |
|
496 |
val special = JEdit_Lib.special_key(evt) |
|
497 |
if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) { |
|
498 |
process_delay.revoke() |
|
499 |
action() |
|
500 |
} |
|
501 |
else process_delay.invoke() |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
private val process_delay = |
|
71704 | 507 |
Delay.last(PIDE.options.seconds("jedit_completion_delay"), gui = true) { |
53784 | 508 |
action() |
509 |
} |
|
510 |
||
75393 | 511 |
override def processKeyEvent(evt0: KeyEvent): Unit = { |
53784 | 512 |
val evt = KeyEventWorkaround.processKeyEvent(evt0) |
513 |
if (evt != null) { |
|
514 |
evt.getID match { |
|
515 |
case KeyEvent.KEY_PRESSED => |
|
516 |
val key_code = evt.getKeyCode |
|
517 |
if (key_code == KeyEvent.VK_ESCAPE) { |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
518 |
if (dismissed()) evt.consume() |
53784 | 519 |
} |
520 |
case KeyEvent.KEY_TYPED => |
|
521 |
super.processKeyEvent(evt) |
|
522 |
process(evt) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
523 |
evt.consume() |
53784 | 524 |
case _ => |
525 |
} |
|
526 |
if (!evt.isConsumed) super.processKeyEvent(evt) |
|
527 |
} |
|
528 |
} |
|
529 |
} |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
530 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
531 |
|
53233 | 532 |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
533 |
class Completion_Popup private( |
56605 | 534 |
opt_range: Option[Text.Range], |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
535 |
layered: JLayeredPane, |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
536 |
location: Point, |
53272 | 537 |
font: Font, |
75393 | 538 |
items: List[Completion_Popup.Item] |
539 |
) extends JPanel(new BorderLayout) { |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
540 |
completion => |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
541 |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
542 |
GUI_Thread.require {} |
53405 | 543 |
|
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
72974
diff
changeset
|
544 |
require(items.nonEmpty, "no completion items") |
71601 | 545 |
val multi: Boolean = items.length > 1 |
53232 | 546 |
|
547 |
||
548 |
/* actions */ |
|
549 |
||
73340 | 550 |
def complete(item: Completion.Item): Unit = {} |
551 |
def propagate(evt: KeyEvent): Unit = {} |
|
552 |
def shutdown(focus: Boolean): Unit = {} |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
553 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
554 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
555 |
/* list view */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
556 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
557 |
private val list_view = new ListView(items) |
53272 | 558 |
list_view.font = font |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
559 |
list_view.selection.intervalMode = ListView.IntervalMode.Single |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
560 |
list_view.peer.setFocusTraversalKeysEnabled(false) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
561 |
list_view.peer.setVisibleRowCount(items.length min 8) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
562 |
list_view.peer.setSelectedIndex(0) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
563 |
|
53398
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
564 |
for (cond <- |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
565 |
List(JComponent.WHEN_FOCUSED, |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
566 |
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
567 |
JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null) |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
568 |
|
75393 | 569 |
private def complete_selected(): Boolean = { |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
570 |
list_view.selection.items.toList match { |
55978 | 571 |
case item :: _ => complete(item.item); true |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
572 |
case _ => false |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
573 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
574 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
575 |
|
75393 | 576 |
private def move_items(n: Int): Unit = { |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
577 |
val i = list_view.peer.getSelectedIndex |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
578 |
val j = ((i + n) min (items.length - 1)) max 0 |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
579 |
if (i != j) { |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
580 |
list_view.peer.setSelectedIndex(j) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
581 |
list_view.peer.ensureIndexIsVisible(j) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
582 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
583 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
584 |
|
75393 | 585 |
private def move_pages(n: Int): Unit = { |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
586 |
val page_size = list_view.peer.getVisibleRowCount - 1 |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
587 |
move_items(page_size * n) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
588 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
589 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
590 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
591 |
/* event handling */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
592 |
|
71601 | 593 |
val inner_key_listener: KeyListener = |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
594 |
JEdit_Lib.key_listener( |
75394 | 595 |
key_pressed = { (e: KeyEvent) => |
75393 | 596 |
if (!e.isConsumed) { |
597 |
e.getKeyCode match { |
|
598 |
case KeyEvent.VK_ENTER if PIDE.options.bool("jedit_completion_select_enter") => |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
599 |
if (complete_selected()) e.consume() |
75393 | 600 |
hide_popup() |
601 |
case KeyEvent.VK_TAB if PIDE.options.bool("jedit_completion_select_tab") => |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
602 |
if (complete_selected()) e.consume() |
75393 | 603 |
hide_popup() |
604 |
case KeyEvent.VK_ESCAPE => |
|
605 |
hide_popup() |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
606 |
e.consume() |
75393 | 607 |
case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi => |
608 |
move_items(-1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
609 |
e.consume() |
75393 | 610 |
case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi => |
611 |
move_items(1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
612 |
e.consume() |
75393 | 613 |
case KeyEvent.VK_PAGE_UP if multi => |
614 |
move_pages(-1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
615 |
e.consume() |
75393 | 616 |
case KeyEvent.VK_PAGE_DOWN if multi => |
617 |
move_pages(1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
618 |
e.consume() |
75393 | 619 |
case _ => |
620 |
if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown) |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
621 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
622 |
} |
75393 | 623 |
} |
624 |
propagate(e) |
|
625 |
}, |
|
71601 | 626 |
key_typed = propagate |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
627 |
) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
628 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
629 |
list_view.peer.addKeyListener(inner_key_listener) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
630 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
631 |
list_view.peer.addMouseListener(new MouseAdapter { |
75393 | 632 |
override def mouseClicked(e: MouseEvent): Unit = { |
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
633 |
if (complete_selected()) e.consume() |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
634 |
hide_popup() |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
635 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
636 |
}) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
637 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
638 |
list_view.peer.addFocusListener(new FocusAdapter { |
73340 | 639 |
override def focusLost(e: FocusEvent): Unit = hide_popup() |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
640 |
}) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
641 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
642 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
643 |
/* main content */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
644 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
645 |
override def getFocusTraversalKeysEnabled = false |
53297 | 646 |
completion.setBorder(new LineBorder(Color.BLACK)) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
647 |
completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent]) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
648 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
649 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
650 |
/* popup */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
651 |
|
56605 | 652 |
def active_range: Option[Text.Range] = |
653 |
if (isDisplayable) opt_range else None |
|
654 |
||
75393 | 655 |
private val popup = { |
72974 | 656 |
val screen = GUI.screen_location(layered, location) |
75393 | 657 |
val size = { |
53230 | 658 |
val geometry = JEdit_Lib.window_geometry(completion, completion) |
64621 | 659 |
val bounds = JEdit_Rendering.popup_bounds |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
660 |
val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
661 |
val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
662 |
new Dimension(w, h) |
53230 | 663 |
} |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
664 |
new Popup(layered, completion, screen.relative(layered, size), size) |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
665 |
} |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
666 |
|
75393 | 667 |
private def show_popup(focus: Boolean): Unit = { |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
668 |
popup.show |
73367 | 669 |
if (focus) list_view.requestFocus() |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
670 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
671 |
|
75393 | 672 |
private def hide_popup(): Unit = { |
56197
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
673 |
shutdown(list_view.peer.isFocusOwner) |
416f7a00e4cb
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
wenzelm
parents:
56177
diff
changeset
|
674 |
popup.hide |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
675 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
676 |
} |