author | wenzelm |
Fri, 12 Aug 2022 11:18:22 +0200 | |
changeset 75807 | b0394e7d43ea |
parent 75394 | 42267c650205 |
child 76765 | c654103e9c9d |
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)), |
66158 | 124 |
rendering.path_completion(caret), |
66152 | 125 |
Document_Model.bibtex_completion(Completion.History.empty, rendering, caret)) |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
126 |
.map(_.range) |
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
127 |
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
|
128 |
case None => range0 |
56175 | 129 |
case Some(Text.Info(_, Completion.No_Completion)) => None |
130 |
case Some(Text.Info(range1, _: Completion.Names)) => Some(range1) |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
131 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
132 |
case _ => None |
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 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
135 |
else None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
136 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
137 |
}.map(range => Text.Info(range, rendering.completion_color)) |
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 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
140 |
/* syntax completion */ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
141 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
142 |
def syntax_completion( |
56175 | 143 |
history: Completion.History, |
144 |
explicit: Boolean, |
|
75393 | 145 |
opt_rendering: Option[JEdit_Rendering] |
146 |
): Option[Completion.Result] = { |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
147 |
val buffer = text_area.getBuffer |
66055 | 148 |
val unicode = Isabelle_Encoding.is_active(buffer) |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
149 |
|
59074 | 150 |
Isabelle.buffer_syntax(buffer) match { |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
151 |
case Some(syntax) => |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
152 |
val context = |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56586
diff
changeset
|
153 |
(for { |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56586
diff
changeset
|
154 |
rendering <- opt_rendering |
56842
b6e266574b26
yet another completion option, to imitate old less ambitious behavior;
wenzelm
parents:
56841
diff
changeset
|
155 |
if PIDE.options.bool("jedit_completion_context") |
66117 | 156 |
caret_range = rendering.before_caret_range(text_area.getCaretPosition) |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65999
diff
changeset
|
157 |
context <- rendering.language_context(caret_range) |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56586
diff
changeset
|
158 |
} yield context) getOrElse syntax.language_context |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
159 |
|
56589
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
for { |
67014 | 164 |
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
|
165 |
result <- |
67005 | 166 |
syntax.complete( |
66055 | 167 |
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
|
168 |
} yield result |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
169 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
170 |
case None => None |
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 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
174 |
|
56170
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
175 |
/* completion action: text area */ |
53273 | 176 |
|
75393 | 177 |
private def insert(item: Completion.Item): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
178 |
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
|
179 |
|
53242 | 180 |
val buffer = text_area.getBuffer |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
181 |
val range = item.range |
56863 | 182 |
if (buffer.isEditable) { |
53242 | 183 |
JEdit_Lib.buffer_edit(buffer) { |
67014 | 184 |
JEdit_Lib.get_text(buffer, range) match { |
53242 | 185 |
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
|
186 |
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
|
187 |
|
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
188 |
/*rectangular selection as "tall caret"*/ |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
189 |
case selection : Selection.Rect |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
190 |
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
|
191 |
text_area.moveCaretPosition(range.stop) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
192 |
(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
|
193 |
.foreach(_ => text_area.backspace()) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
194 |
text_area.setSelectedText(selection, item.replacement) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
195 |
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
|
196 |
|
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
197 |
/*other selections: rectangular, multiple range, ...*/ |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
198 |
case selection |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
199 |
if selection != null && |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
|
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
205 |
/*no selection*/ |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
206 |
case _ => |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
207 |
buffer.remove(range.start, range.length) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
208 |
buffer.insert(range.start, item.replacement) |
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
209 |
text_area.moveCaretPosition(range.start + item.replacement.length + item.move) |
66180 | 210 |
Isabelle.indent_input(text_area) |
56325
ffbfc92e6508
special treatment for various kinds of selections: imitate normal flow of editing;
wenzelm
parents:
56197
diff
changeset
|
211 |
} |
53242 | 212 |
case _ => |
213 |
} |
|
53233 | 214 |
} |
215 |
} |
|
216 |
} |
|
53242 | 217 |
|
56586 | 218 |
def action( |
219 |
immediate: Boolean = false, |
|
220 |
explicit: Boolean = false, |
|
221 |
delayed: Boolean = false, |
|
75393 | 222 |
word_only: Boolean = false |
223 |
): Boolean = { |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
224 |
val view = text_area.getView |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
225 |
val layered = view.getLayeredPane |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
226 |
val buffer = text_area.getBuffer |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
227 |
val painter = text_area.getPainter |
53273 | 228 |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
229 |
val history = PIDE.plugin.completion_history.value |
66055 | 230 |
val unicode = Isabelle_Encoding.is_active(buffer) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
231 |
|
75393 | 232 |
def open_popup(result: Completion.Result): Unit = { |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
233 |
val font = |
55825 | 234 |
painter.getFont.deriveFont( |
235 |
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
|
236 |
|
55711 | 237 |
val range = result.range |
238 |
||
239 |
val loc1 = text_area.offsetToXY(range.start) |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
240 |
if (loc1 != null) { |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
241 |
val loc2 = |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
242 |
SwingUtilities.convertPoint(painter, |
58767
30766b5fd0e1
proper line height and text base line, like regular TextAreaPainter.PaintText;
wenzelm
parents:
58592
diff
changeset
|
243 |
loc1.x, loc1.y + painter.getLineHeight, layered) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
244 |
|
55978 | 245 |
val items = result.items.map(new Item(_)) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
246 |
val completion = |
75393 | 247 |
new Completion_Popup(Some(range), layered, loc2, font, items) { |
248 |
override def complete(item: Completion.Item): Unit = { |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
249 |
PIDE.plugin.completion_history.update(item) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
250 |
insert(item) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
251 |
} |
75393 | 252 |
override def propagate(evt: KeyEvent): Unit = { |
56840
879fe16bd27c
propagate more events, notably after hide_popup (e.g. LEFT, RIGHT);
wenzelm
parents:
56662
diff
changeset
|
253 |
if (view.getKeyEventInterceptor == null) |
879fe16bd27c
propagate more events, notably after hide_popup (e.g. LEFT, RIGHT);
wenzelm
parents:
56662
diff
changeset
|
254 |
JEdit_Lib.propagate_key(view, evt) |
879fe16bd27c
propagate more events, notably after hide_popup (e.g. LEFT, RIGHT);
wenzelm
parents:
56662
diff
changeset
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
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
|
259 |
} |
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 |
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
|
261 |
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
|
262 |
} |
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
|
263 |
} |
56171
15351577da10
clarified key event propagation, in accordance to outer_key_listener;
wenzelm
parents:
56170
diff
changeset
|
264 |
if (evt.getID == KeyEvent.KEY_TYPED) input(evt) |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
265 |
} |
75393 | 266 |
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
|
267 |
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
|
268 |
view.setKeyEventInterceptor(null) |
73367 | 269 |
if (focus) text_area.requestFocus() |
56606
68b7a6db4a32
avoid ooddity: invoke intended function instead of java.awt.Container.invalidate();
wenzelm
parents:
56605
diff
changeset
|
270 |
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
|
271 |
} |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
272 |
} |
57127
a406e15c3cf7
make double-sure that old popup is dismissed, before replacing it;
wenzelm
parents:
57051
diff
changeset
|
273 |
dismissed() |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
274 |
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
|
275 |
view.setKeyEventInterceptor(completion.inner_key_listener) |
56606
68b7a6db4a32
avoid ooddity: invoke intended function instead of java.awt.Container.invalidate();
wenzelm
parents:
56605
diff
changeset
|
276 |
JEdit_Lib.invalidate_range(text_area, range) |
56841 | 277 |
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
|
278 |
completion.show_popup(false) |
55693
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 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
281 |
|
56175 | 282 |
if (buffer.isEditable) { |
66117 | 283 |
val caret = text_area.getCaretPosition |
64882 | 284 |
val opt_rendering = Document_View.get(text_area).map(_.get_rendering()) |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
285 |
val result0 = syntax_completion(history, explicit, opt_rendering) |
75393 | 286 |
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
|
287 |
opt_rendering match { |
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
288 |
case Some(rendering) => |
66055 | 289 |
rendering.semantic_completion_result(history, unicode, result0.map(_.range), |
66117 | 290 |
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
|
291 |
case None => (false, None) |
56175 | 292 |
} |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
293 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
294 |
if (no_completion) false |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
295 |
else { |
75393 | 296 |
val result = { |
61601
15952a05133c
syntactic completion may supersede semantic completion, e.g. relevant for "\undefined" vs. "undefined" in ML;
wenzelm
parents:
59319
diff
changeset
|
297 |
val result1 = |
56586 | 298 |
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
|
299 |
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
|
300 |
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
|
301 |
case None => result1 |
56564
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56326
diff
changeset
|
302 |
case Some(rendering) => |
66157 | 303 |
Completion.Result.merge(history, |
66151 | 304 |
result1, |
305 |
JEdit_Spell_Checker.completion(rendering, explicit, caret), |
|
66158 | 306 |
rendering.path_completion(caret), |
66152 | 307 |
Document_Model.bibtex_completion(history, rendering, caret)) |
56564
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56326
diff
changeset
|
308 |
} |
56586 | 309 |
} |
56175 | 310 |
result match { |
311 |
case Some(result) => |
|
312 |
result.items match { |
|
313 |
case List(item) if result.unique && item.immediate && immediate => |
|
314 |
insert(item) |
|
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
315 |
true |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
316 |
case _ :: _ if !delayed => |
56175 | 317 |
open_popup(result) |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
318 |
false |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
319 |
case _ => false |
56175 | 320 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
321 |
case None => false |
56175 | 322 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
323 |
} |
55752 | 324 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
325 |
else false |
53292
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 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
328 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
329 |
/* input key events */ |
53273 | 330 |
|
75393 | 331 |
def input(evt: KeyEvent): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
332 |
GUI_Thread.require {} |
53233 | 333 |
|
66180 | 334 |
if (!evt.isConsumed) { |
335 |
val special = JEdit_Lib.special_key(evt) |
|
336 |
||
337 |
if (PIDE.options.bool("jedit_completion")) { |
|
53273 | 338 |
dismissed() |
66185 | 339 |
if (evt.getKeyChar != '\b') { |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
340 |
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
|
341 |
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
|
342 |
input_delay.revoke() |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
343 |
action(immediate = immediate) |
53397
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
344 |
} |
56326
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
345 |
else { |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
346 |
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
|
347 |
input_delay.revoke() |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
348 |
else |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
349 |
input_delay.invoke() |
c3d7b3bb2708
immediate completion even with delay, which is the default according to 638b29331549;
wenzelm
parents:
56325
diff
changeset
|
350 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
351 |
} |
53242 | 352 |
} |
66180 | 353 |
|
66186 | 354 |
val selection = text_area.getSelection() |
71383 | 355 |
if (!special && (selection == null || selection.isEmpty)) |
66186 | 356 |
Isabelle.indent_input(text_area) |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
357 |
} |
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
358 |
} |
53273 | 359 |
|
360 |
private val input_delay = |
|
71704 | 361 |
Delay.last(PIDE.options.seconds("jedit_completion_delay"), gui = true) { |
53322 | 362 |
action() |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
363 |
} |
53273 | 364 |
|
365 |
||
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
366 |
/* dismiss popup */ |
53273 | 367 |
|
75393 | 368 |
def dismissed(): Boolean = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
369 |
GUI_Thread.require {} |
53273 | 370 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
371 |
completion_popup match { |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
372 |
case Some(completion) => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
373 |
completion.hide_popup() |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
374 |
completion_popup = None |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
375 |
true |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
376 |
case None => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
377 |
false |
53273 | 378 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
379 |
} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
380 |
|
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 |
/* activation */ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
383 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
384 |
private val outer_key_listener = |
71601 | 385 |
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
|
386 |
|
73340 | 387 |
private def activate(): Unit = |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
388 |
text_area.addKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
389 |
|
75393 | 390 |
private def deactivate(): Unit = { |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
391 |
dismissed() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
392 |
text_area.removeKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
393 |
} |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
394 |
} |
53784 | 395 |
|
396 |
||
397 |
||
398 |
/** history text field **/ |
|
399 |
||
400 |
class History_Text_Field( |
|
401 |
name: String = null, |
|
402 |
instant_popups: Boolean = false, |
|
403 |
enter_adds_to_history: Boolean = true, |
|
67004
af72fa58f71b
clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents:
66186
diff
changeset
|
404 |
syntax: Outer_Syntax = Outer_Syntax.empty) extends |
75393 | 405 |
HistoryTextField(name, instant_popups, enter_adds_to_history |
406 |
) { |
|
53784 | 407 |
text_field => |
408 |
||
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
|
409 |
// 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
|
410 |
if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret) |
53784 | 411 |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
412 |
// owned by GUI thread |
53784 | 413 |
private var completion_popup: Option[Completion_Popup] = None |
414 |
||
415 |
||
416 |
/* dismiss */ |
|
417 |
||
75393 | 418 |
private def dismissed(): Boolean = { |
53784 | 419 |
completion_popup match { |
420 |
case Some(completion) => |
|
421 |
completion.hide_popup() |
|
422 |
completion_popup = None |
|
423 |
true |
|
424 |
case None => |
|
425 |
false |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
||
430 |
/* insert */ |
|
431 |
||
75393 | 432 |
private def insert(item: Completion.Item): Unit = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
433 |
GUI_Thread.require {} |
53784 | 434 |
|
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
435 |
val range = item.range |
56863 | 436 |
if (text_field.isEditable) { |
53784 | 437 |
val content = text_field.getText |
66114 | 438 |
range.try_substring(content) match { |
53784 | 439 |
case Some(text) if text == item.original => |
440 |
text_field.setText( |
|
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
441 |
content.substring(0, range.start) + |
53784 | 442 |
item.replacement + |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
443 |
content.substring(range.stop)) |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
444 |
text_field.getCaret.setDot(range.start + item.replacement.length + item.move) |
53784 | 445 |
case _ => |
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
||
56170
638b29331549
allow implicit semantic completion, notably after delay that exceeds usual round-trip time;
wenzelm
parents:
55978
diff
changeset
|
451 |
/* completion action: text field */ |
53784 | 452 |
|
75393 | 453 |
def action(): Unit = { |
53784 | 454 |
GUI.layered_pane(text_field) match { |
455 |
case Some(layered) if text_field.isEditable => |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
456 |
val history = PIDE.plugin.completion_history.value |
53784 | 457 |
|
458 |
val caret = text_field.getCaret.getDot |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
459 |
val text = text_field.getText |
55748 | 460 |
|
55749 | 461 |
val context = syntax.language_context |
55748 | 462 |
|
67005 | 463 |
syntax.complete(history, true, false, 0, text, caret, context) match { |
53784 | 464 |
case Some(result) => |
465 |
val fm = text_field.getFontMetrics(text_field.getFont) |
|
466 |
val loc = |
|
467 |
SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered) |
|
468 |
||
55978 | 469 |
val items = result.items.map(new Item(_)) |
55711 | 470 |
val completion = |
75393 | 471 |
new Completion_Popup(None, layered, loc, text_field.getFont, items) { |
472 |
override def complete(item: Completion.Item): Unit = { |
|
65239
509a9b0ad02e
avoid global variables with implicit initialization;
wenzelm
parents:
65139
diff
changeset
|
473 |
PIDE.plugin.completion_history.update(item) |
55978 | 474 |
insert(item) |
475 |
} |
|
73340 | 476 |
override def propagate(evt: KeyEvent): Unit = |
55978 | 477 |
if (!evt.isConsumed) text_field.processKeyEvent(evt) |
73340 | 478 |
override def shutdown(focus: Boolean): Unit = |
73367 | 479 |
if (focus) text_field.requestFocus() |
53784 | 480 |
} |
57127
a406e15c3cf7
make double-sure that old popup is dismissed, before replacing it;
wenzelm
parents:
57051
diff
changeset
|
481 |
dismissed() |
53784 | 482 |
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
|
483 |
completion.show_popup(true) |
53784 | 484 |
|
485 |
case None => |
|
486 |
} |
|
487 |
case _ => |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
||
492 |
/* process key event */ |
|
493 |
||
75393 | 494 |
private def process(evt: KeyEvent): Unit = { |
53784 | 495 |
if (PIDE.options.bool("jedit_completion")) { |
496 |
dismissed() |
|
497 |
if (evt.getKeyChar != '\b') { |
|
498 |
val special = JEdit_Lib.special_key(evt) |
|
499 |
if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) { |
|
500 |
process_delay.revoke() |
|
501 |
action() |
|
502 |
} |
|
503 |
else process_delay.invoke() |
|
504 |
} |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
private val process_delay = |
|
71704 | 509 |
Delay.last(PIDE.options.seconds("jedit_completion_delay"), gui = true) { |
53784 | 510 |
action() |
511 |
} |
|
512 |
||
75393 | 513 |
override def processKeyEvent(evt0: KeyEvent): Unit = { |
53784 | 514 |
val evt = KeyEventWorkaround.processKeyEvent(evt0) |
515 |
if (evt != null) { |
|
516 |
evt.getID match { |
|
517 |
case KeyEvent.KEY_PRESSED => |
|
518 |
val key_code = evt.getKeyCode |
|
519 |
if (key_code == KeyEvent.VK_ESCAPE) { |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
520 |
if (dismissed()) evt.consume() |
53784 | 521 |
} |
522 |
case KeyEvent.KEY_TYPED => |
|
523 |
super.processKeyEvent(evt) |
|
524 |
process(evt) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
525 |
evt.consume() |
53784 | 526 |
case _ => |
527 |
} |
|
528 |
if (!evt.isConsumed) super.processKeyEvent(evt) |
|
529 |
} |
|
530 |
} |
|
531 |
} |
|
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
|
532 |
} |
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 |
|
53233 | 534 |
|
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
|
535 |
class Completion_Popup private( |
56605 | 536 |
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
|
537 |
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
|
538 |
location: Point, |
53272 | 539 |
font: Font, |
75393 | 540 |
items: List[Completion_Popup.Item] |
541 |
) 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
|
542 |
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
|
543 |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57588
diff
changeset
|
544 |
GUI_Thread.require {} |
53405 | 545 |
|
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
72974
diff
changeset
|
546 |
require(items.nonEmpty, "no completion items") |
71601 | 547 |
val multi: Boolean = items.length > 1 |
53232 | 548 |
|
549 |
||
550 |
/* actions */ |
|
551 |
||
73340 | 552 |
def complete(item: Completion.Item): Unit = {} |
553 |
def propagate(evt: KeyEvent): Unit = {} |
|
554 |
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
|
555 |
|
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 |
/* 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
|
558 |
|
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 |
private val list_view = new ListView(items) |
53272 | 560 |
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
|
561 |
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
|
562 |
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
|
563 |
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
|
564 |
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
|
565 |
|
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
|
566 |
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
|
567 |
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
|
568 |
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
|
569 |
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
|
570 |
|
75393 | 571 |
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
|
572 |
list_view.selection.items.toList match { |
55978 | 573 |
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
|
574 |
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
|
575 |
} |
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
|
576 |
} |
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 |
|
75393 | 578 |
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
|
579 |
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
|
580 |
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
|
581 |
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
|
582 |
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
|
583 |
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
|
584 |
} |
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
|
585 |
} |
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 |
|
75393 | 587 |
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
|
588 |
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
|
589 |
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
|
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 |
|
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 |
|
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
|
593 |
/* 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
|
594 |
|
71601 | 595 |
val inner_key_listener: KeyListener = |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
596 |
JEdit_Lib.key_listener( |
75394 | 597 |
key_pressed = { (e: KeyEvent) => |
75393 | 598 |
if (!e.isConsumed) { |
599 |
e.getKeyCode match { |
|
600 |
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
|
601 |
if (complete_selected()) e.consume() |
75393 | 602 |
hide_popup() |
603 |
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
|
604 |
if (complete_selected()) e.consume() |
75393 | 605 |
hide_popup() |
606 |
case KeyEvent.VK_ESCAPE => |
|
607 |
hide_popup() |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
608 |
e.consume() |
75393 | 609 |
case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi => |
610 |
move_items(-1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
611 |
e.consume() |
75393 | 612 |
case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi => |
613 |
move_items(1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
614 |
e.consume() |
75393 | 615 |
case KeyEvent.VK_PAGE_UP if multi => |
616 |
move_pages(-1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
617 |
e.consume() |
75393 | 618 |
case KeyEvent.VK_PAGE_DOWN if multi => |
619 |
move_pages(1) |
|
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
620 |
e.consume() |
75393 | 621 |
case _ => |
622 |
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
|
623 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
624 |
} |
75393 | 625 |
} |
626 |
propagate(e) |
|
627 |
}, |
|
71601 | 628 |
key_typed = propagate |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
629 |
) |
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 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
631 |
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
|
632 |
|
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
|
633 |
list_view.peer.addMouseListener(new MouseAdapter { |
75393 | 634 |
override def mouseClicked(e: MouseEvent): Unit = { |
75807
b0394e7d43ea
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75394
diff
changeset
|
635 |
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
|
636 |
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
|
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 |
}) |
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
|
639 |
|
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 |
list_view.peer.addFocusListener(new FocusAdapter { |
73340 | 641 |
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
|
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 |
|
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 |
/* 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
|
646 |
|
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 |
override def getFocusTraversalKeysEnabled = false |
53297 | 648 |
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
|
649 |
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
|
650 |
|
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 |
|
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
|
652 |
/* 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
|
653 |
|
56605 | 654 |
def active_range: Option[Text.Range] = |
655 |
if (isDisplayable) opt_range else None |
|
656 |
||
75393 | 657 |
private val popup = { |
72974 | 658 |
val screen = GUI.screen_location(layered, location) |
75393 | 659 |
val size = { |
53230 | 660 |
val geometry = JEdit_Lib.window_geometry(completion, completion) |
64621 | 661 |
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
|
662 |
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
|
663 |
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
|
664 |
new Dimension(w, h) |
53230 | 665 |
} |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
666 |
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
|
667 |
} |
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 |
|
75393 | 669 |
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
|
670 |
popup.show |
73367 | 671 |
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
|
672 |
} |
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
|
673 |
|
75393 | 674 |
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
|
675 |
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
|
676 |
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
|
677 |
} |
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
|
678 |
} |