| author | wenzelm |
| Tue, 01 Jul 2014 21:07:02 +0200 | |
| changeset 57480 | d256f49b4799 |
| parent 56878 | a5d082a85124 |
| child 57588 | ff31aad27661 |
| permissions | -rw-r--r-- |
|
55673
0286219c1261
clarified module location (again, see 763d35697338);
wenzelm
parents:
55666
diff
changeset
|
1 |
/* Title: Pure/General/completion.scala |
| 31763 | 2 |
Author: Makarius |
3 |
||
| 55687 | 4 |
Semantic completion within the formal context (reported names). |
| 55674 | 5 |
Syntactic completion of keywords and symbols, with abbreviations |
| 55992 | 6 |
(based on language context). |
7 |
*/ |
|
| 31763 | 8 |
|
9 |
package isabelle |
|
10 |
||
| 55618 | 11 |
|
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
12 |
import scala.collection.immutable.SortedMap |
| 31763 | 13 |
import scala.util.parsing.combinator.RegexParsers |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
14 |
import scala.math.Ordering |
| 31763 | 15 |
|
16 |
||
| 46624 | 17 |
object Completion |
| 31763 | 18 |
{
|
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
19 |
/** completion result **/ |
| 53275 | 20 |
|
|
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
21 |
sealed case class Item( |
|
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55687
diff
changeset
|
22 |
range: Text.Range, |
|
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
23 |
original: String, |
| 55666 | 24 |
name: String, |
| 55978 | 25 |
description: List[String], |
|
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
26 |
replacement: String, |
| 55666 | 27 |
move: Int, |
|
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
28 |
immediate: Boolean) |
| 53275 | 29 |
|
|
55914
c5b752d549e3
clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents:
55813
diff
changeset
|
30 |
object Result |
|
c5b752d549e3
clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents:
55813
diff
changeset
|
31 |
{
|
|
c5b752d549e3
clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents:
55813
diff
changeset
|
32 |
def empty(range: Text.Range): Result = Result(range, "", false, Nil) |
| 56175 | 33 |
def merge(history: History, result1: Option[Result], result2: Option[Result]): Option[Result] = |
34 |
(result1, result2) match {
|
|
35 |
case (_, None) => result1 |
|
36 |
case (None, _) => result2 |
|
37 |
case (Some(res1), Some(res2)) => |
|
38 |
if (res1.range != res2.range || res1.original != res2.original) result1 |
|
39 |
else {
|
|
40 |
val items = (res1.items ::: res2.items).sorted(history.ordering) |
|
41 |
Some(Result(res1.range, res1.original, false, items)) |
|
42 |
} |
|
43 |
} |
|
|
55914
c5b752d549e3
clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents:
55813
diff
changeset
|
44 |
} |
|
c5b752d549e3
clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents:
55813
diff
changeset
|
45 |
|
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
46 |
sealed case class Result( |
|
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
47 |
range: Text.Range, |
|
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
48 |
original: String, |
|
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
49 |
unique: Boolean, |
|
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
50 |
items: List[Item]) |
|
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53324
diff
changeset
|
51 |
|
| 53275 | 52 |
|
| 46624 | 53 |
|
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
54 |
/** persistent history **/ |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
55 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
56 |
private val COMPLETION_HISTORY = Path.explode("$ISABELLE_HOME_USER/etc/completion_history")
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
57 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
58 |
object History |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
59 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
60 |
val empty: History = new History() |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
61 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
62 |
def load(): History = |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
63 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
64 |
def ignore_error(msg: String): Unit = |
|
56782
433cf57550fa
more systematic Isabelle output, like in classic Isabelle/ML (without markup);
wenzelm
parents:
56661
diff
changeset
|
65 |
Output.warning("Ignoring bad content of file " + COMPLETION_HISTORY +
|
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
66 |
(if (msg == "") "" else "\n" + msg)) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
67 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
68 |
val content = |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
69 |
if (COMPLETION_HISTORY.is_file) {
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
70 |
try {
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
71 |
import XML.Decode._ |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
72 |
list(pair(Symbol.decode_string, int))( |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
73 |
YXML.parse_body(File.read(COMPLETION_HISTORY))) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
74 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
75 |
catch {
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
76 |
case ERROR(msg) => ignore_error(msg); Nil |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
77 |
case _: XML.Error => ignore_error(""); Nil
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
78 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
79 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
80 |
else Nil |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
81 |
(empty /: content)(_ + _) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
82 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
83 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
84 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
85 |
final class History private(rep: SortedMap[String, Int] = SortedMap.empty) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
86 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
87 |
override def toString: String = rep.mkString("Completion.History(", ",", ")")
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
88 |
|
|
56878
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
89 |
def frequency(name: String): Int = |
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
90 |
default_frequency(Symbol.encode(name)) getOrElse |
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
91 |
rep.getOrElse(name, 0) |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
92 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
93 |
def + (entry: (String, Int)): History = |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
94 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
95 |
val (name, freq) = entry |
|
56564
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56278
diff
changeset
|
96 |
if (name == "") this |
|
94c55cc73747
added spell-checker completion dialog, without counting frequency of items due to empty name;
wenzelm
parents:
56278
diff
changeset
|
97 |
else new History(rep + (name -> (frequency(name) + freq))) |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
98 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
99 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
100 |
def ordering: Ordering[Item] = |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
101 |
new Ordering[Item] {
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
102 |
def compare(item1: Item, item2: Item): Int = |
|
56162
ea6303e2261b
clarified completion ordering: prefer local names;
wenzelm
parents:
56042
diff
changeset
|
103 |
frequency(item2.name) compare frequency(item1.name) |
|
ea6303e2261b
clarified completion ordering: prefer local names;
wenzelm
parents:
56042
diff
changeset
|
104 |
} |
|
ea6303e2261b
clarified completion ordering: prefer local names;
wenzelm
parents:
56042
diff
changeset
|
105 |
|
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
106 |
def save() |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
107 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
108 |
Isabelle_System.mkdirs(COMPLETION_HISTORY.dir) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
109 |
File.write_backup(COMPLETION_HISTORY, |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
110 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
111 |
import XML.Encode._ |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
112 |
YXML.string_of_body(list(pair(Symbol.encode_string, int))(rep.toList)) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
113 |
}) |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
114 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
115 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
116 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
117 |
class History_Variable |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
118 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
119 |
private var history = History.empty |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
120 |
def value: History = synchronized { history }
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
121 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
122 |
def load() |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
123 |
{
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
124 |
val h = History.load() |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
125 |
synchronized { history = h }
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
126 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
127 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
128 |
def update(item: Item, freq: Int = 1): Unit = synchronized {
|
| 55666 | 129 |
history = history + (item.name -> freq) |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
130 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
131 |
} |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
132 |
|
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
133 |
|
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
134 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
135 |
/** semantic completion **/ |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
136 |
|
| 56173 | 137 |
object Semantic |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
138 |
{
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
139 |
object Info |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
140 |
{
|
| 56173 | 141 |
def unapply(info: Text.Markup): Option[Text.Info[Semantic]] = |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
142 |
info.info match {
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
143 |
case XML.Elem(Markup(Markup.COMPLETION, _), body) => |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
144 |
try {
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
145 |
val (total, names) = |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
146 |
{
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
147 |
import XML.Decode._ |
| 55977 | 148 |
pair(int, list(pair(string, pair(string, string))))(body) |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
149 |
} |
| 56173 | 150 |
Some(Text.Info(info.range, Names(total, names))) |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
151 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
152 |
catch { case _: XML.Error => None }
|
|
55914
c5b752d549e3
clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents:
55813
diff
changeset
|
153 |
case XML.Elem(Markup(Markup.NO_COMPLETION, _), _) => |
| 56173 | 154 |
Some(Text.Info(info.range, No_Completion)) |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
155 |
case _ => None |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
156 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
157 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
158 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
159 |
|
| 56173 | 160 |
sealed abstract class Semantic |
| 56175 | 161 |
case object No_Completion extends Semantic |
162 |
case class Names(total: Int, names: List[(String, (String, String))]) extends Semantic |
|
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
163 |
{
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
164 |
def complete( |
| 56173 | 165 |
range: Text.Range, |
166 |
history: Completion.History, |
|
167 |
do_decode: Boolean, |
|
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
168 |
original: String): Option[Completion.Result] = |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
169 |
{
|
| 55977 | 170 |
def decode(s: String): String = if (do_decode) Symbol.decode(s) else s |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
171 |
val items = |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
172 |
for {
|
| 55977 | 173 |
(xname, (kind, name)) <- names |
174 |
xname1 = decode(xname) |
|
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
175 |
if xname1 != original |
| 55977 | 176 |
(full_name, descr_name) = |
177 |
if (kind == "") (name, quote(decode(name))) |
|
| 56600 | 178 |
else |
| 56800 | 179 |
(Long_Name.qualify(kind, name), |
| 56600 | 180 |
Word.implode(Word.explode('_', kind)) + " " + quote(decode(name)))
|
| 55978 | 181 |
description = List(xname1, "(" + descr_name + ")")
|
| 55977 | 182 |
} yield Item(range, original, full_name, description, xname1, 0, true) |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
183 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
184 |
if (items.isEmpty) None |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
185 |
else Some(Result(range, original, names.length == 1, items.sorted(history.ordering))) |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
186 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
187 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
188 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
189 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
190 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
191 |
/** syntactic completion **/ |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
192 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
193 |
/* language context */ |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
194 |
|
| 55749 | 195 |
object Language_Context |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
196 |
{
|
| 55749 | 197 |
val outer = Language_Context("", true, false)
|
198 |
val inner = Language_Context(Markup.Language.UNKNOWN, true, false) |
|
199 |
val ML_outer = Language_Context(Markup.Language.ML, false, true) |
|
200 |
val ML_inner = Language_Context(Markup.Language.ML, true, false) |
|
|
56278
2576d3a40ed6
separate tokenization and language context for SML: no symbols, no antiquotes;
wenzelm
parents:
56221
diff
changeset
|
201 |
val SML_outer = Language_Context(Markup.Language.SML, false, false) |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
202 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
203 |
|
| 55749 | 204 |
sealed case class Language_Context(language: String, symbols: Boolean, antiquotes: Boolean) |
|
55694
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
205 |
{
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
206 |
def is_outer: Boolean = language == "" |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
207 |
} |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
208 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
209 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
210 |
/* init */ |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
211 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
212 |
val empty: Completion = new Completion() |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
213 |
def init(): Completion = empty.add_symbols() |
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
214 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
215 |
|
|
a1184dfb8e00
clarified semantic completion: retain kind.full_name as official item name for history;
wenzelm
parents:
55693
diff
changeset
|
216 |
/* word parsers */ |
| 31763 | 217 |
|
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
218 |
private object Word_Parsers extends RegexParsers |
| 31763 | 219 |
{
|
220 |
override val whiteSpace = "".r |
|
221 |
||
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
222 |
private def reverse_symbol: Parser[String] = """>[A-Za-z0-9_']+\^?<\\""".r |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
223 |
private def reverse_symb: Parser[String] = """[A-Za-z0-9_']{2,}\^?<\\""".r
|
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
224 |
private def escape: Parser[String] = """[a-zA-Z0-9_']+\\""".r |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
225 |
|
|
56039
5ff5208de089
include long identifiers in the reckoning of words (e.g. "integer.lifting" vs. 'lifting_update');
wenzelm
parents:
56001
diff
changeset
|
226 |
private val word_regex = "[a-zA-Z0-9_'.]+".r |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
227 |
private def word: Parser[String] = word_regex |
|
56039
5ff5208de089
include long identifiers in the reckoning of words (e.g. "integer.lifting" vs. 'lifting_update');
wenzelm
parents:
56001
diff
changeset
|
228 |
private def word3: Parser[String] = "[a-zA-Z0-9_'.]{3,}".r
|
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
229 |
private def underscores: Parser[String] = "_*".r |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
230 |
|
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
231 |
def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches |
|
56042
d3c35a300433
proper Char comparison, despite weakly-typed Scala (cf. 5ff5208de089);
wenzelm
parents:
56039
diff
changeset
|
232 |
def is_word_char(c: Char): Boolean = Symbol.is_ascii_letdig(c) || c == '.' |
| 31763 | 233 |
|
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
234 |
def extend_word(text: CharSequence, offset: Text.Offset): Text.Offset = |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
235 |
{
|
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
236 |
val n = text.length |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
237 |
var i = offset |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
238 |
while (i < n && is_word_char(text.charAt(i))) i += 1 |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
239 |
if (i < n && text.charAt(i) == '>' && read_symbol(text.subSequence(0, i + 1)).isDefined) |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
240 |
i + 1 |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
241 |
else i |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
242 |
} |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
243 |
|
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
244 |
def read_symbol(in: CharSequence): Option[String] = |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
245 |
{
|
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
246 |
val reverse_in = new Library.Reverse(in) |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
247 |
parse(reverse_symbol ^^ (_.reverse), reverse_in) match {
|
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
248 |
case Success(result, _) => Some(result) |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
249 |
case _ => None |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
250 |
} |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
251 |
} |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
252 |
|
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
253 |
def read_word(explicit: Boolean, in: CharSequence): Option[(String, String)] = |
| 31763 | 254 |
{
|
| 53322 | 255 |
val parse_word = if (explicit) word else word3 |
|
37072
9105c8237c7a
renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents:
36012
diff
changeset
|
256 |
val reverse_in = new Library.Reverse(in) |
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
257 |
val parser = |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
258 |
(reverse_symbol | reverse_symb | escape) ^^ (x => (x.reverse, "")) | |
|
56221
a8dfbe9f25da
accomodate word as part of schematic variable name;
wenzelm
parents:
56175
diff
changeset
|
259 |
underscores ~ parse_word ~ opt("?") ^^
|
|
a8dfbe9f25da
accomodate word as part of schematic variable name;
wenzelm
parents:
56175
diff
changeset
|
260 |
{ case x ~ y ~ z => (z.getOrElse("") + y.reverse, x) }
|
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
261 |
parse(parser, reverse_in) match {
|
| 31763 | 262 |
case Success(result, _) => Some(result) |
263 |
case _ => None |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
| 55666 | 267 |
|
268 |
||
269 |
/* abbreviations */ |
|
270 |
||
|
56661
ef623f6f036b
avoid octal escape literals -- deprecated in scala-2.11.0;
wenzelm
parents:
56600
diff
changeset
|
271 |
private val caret_indicator = '\u0007' |
| 55666 | 272 |
private val antiquote = "@{"
|
|
56878
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
273 |
|
| 55666 | 274 |
private val default_abbrs = |
|
56878
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
275 |
List("@{" -> "@{\u0007}",
|
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
276 |
"`" -> "\\<close>", |
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
277 |
"`" -> "\\<open>", |
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
278 |
"`" -> "\\<open>\u0007\\<close>") |
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
279 |
|
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
280 |
private def default_frequency(name: String): Option[Int] = |
|
a5d082a85124
hardwired default_frequency to avoid fluctuation of popup content;
wenzelm
parents:
56800
diff
changeset
|
281 |
default_abbrs.iterator.map(_._2).zipWithIndex.find(_._1 == name).map(_._2) |
| 31763 | 282 |
} |
283 |
||
| 46712 | 284 |
final class Completion private( |
| 55984 | 285 |
keywords: Map[String, Boolean] = Map.empty, |
| 46625 | 286 |
words_lex: Scan.Lexicon = Scan.Lexicon.empty, |
| 53318 | 287 |
words_map: Multi_Map[String, String] = Multi_Map.empty, |
| 46625 | 288 |
abbrevs_lex: Scan.Lexicon = Scan.Lexicon.empty, |
| 53318 | 289 |
abbrevs_map: Multi_Map[String, (String, String)] = Multi_Map.empty) |
| 31763 | 290 |
{
|
| 55983 | 291 |
/* keywords */ |
| 31763 | 292 |
|
|
55986
61b0021ed674
more strict discrimination: symbols vs. keywords could overlap;
wenzelm
parents:
55985
diff
changeset
|
293 |
private def is_symbol(name: String): Boolean = Symbol.names.isDefinedAt(name) |
|
61b0021ed674
more strict discrimination: symbols vs. keywords could overlap;
wenzelm
parents:
55985
diff
changeset
|
294 |
private def is_keyword(name: String): Boolean = !is_symbol(name) && keywords.isDefinedAt(name) |
|
56001
2df1e7bca361
do allow replacement of words where this is appropriate, notably symbol abbrevs and keyword templates (see also 1c42ebdb3a58);
wenzelm
parents:
55996
diff
changeset
|
295 |
private def is_keyword_template(name: String, template: Boolean): Boolean = |
|
2df1e7bca361
do allow replacement of words where this is appropriate, notably symbol abbrevs and keyword templates (see also 1c42ebdb3a58);
wenzelm
parents:
55996
diff
changeset
|
296 |
is_keyword(name) && keywords(name) == template |
| 55984 | 297 |
|
298 |
def + (keyword: String, template: String): Completion = |
|
| 46624 | 299 |
new Completion( |
| 55984 | 300 |
keywords + (keyword -> (keyword != template)), |
| 46624 | 301 |
words_lex + keyword, |
| 55984 | 302 |
words_map + (keyword -> template), |
| 46624 | 303 |
abbrevs_lex, |
304 |
abbrevs_map) |
|
| 31763 | 305 |
|
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
306 |
def + (keyword: String): Completion = this + (keyword, keyword) |
|
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
307 |
|
| 55983 | 308 |
|
309 |
/* symbols with abbreviations */ |
|
310 |
||
| 46624 | 311 |
private def add_symbols(): Completion = |
| 31763 | 312 |
{
|
313 |
val words = |
|
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
314 |
(for ((x, _) <- Symbol.names.toList) yield (x, x)) ::: |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
315 |
(for ((x, y) <- Symbol.names.toList) yield ("\\" + y, x)) :::
|
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
316 |
(for ((x, y) <- Symbol.abbrevs.toList if Completion.Word_Parsers.is_word(y)) yield (y, x)) |
| 31763 | 317 |
|
| 55666 | 318 |
val symbol_abbrs = |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
319 |
(for ((x, y) <- Symbol.abbrevs.iterator if !Completion.Word_Parsers.is_word(y)) |
| 55666 | 320 |
yield (y, x)).toList |
321 |
||
322 |
val abbrs = |
|
|
56591
1a59587f46ec
clarified abbreviations for cartouche delimiters, to work in any context;
wenzelm
parents:
56564
diff
changeset
|
323 |
for ((a, b) <- symbol_abbrs ::: Completion.default_abbrs) |
| 55666 | 324 |
yield (a.reverse, (a, b)) |
| 31763 | 325 |
|
| 46624 | 326 |
new Completion( |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
327 |
keywords, |
| 46624 | 328 |
words_lex ++ words.map(_._1), |
329 |
words_map ++ words, |
|
330 |
abbrevs_lex ++ abbrs.map(_._1), |
|
331 |
abbrevs_map ++ abbrs) |
|
| 31763 | 332 |
} |
333 |
||
334 |
||
335 |
/* complete */ |
|
336 |
||
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
337 |
def complete( |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
338 |
history: Completion.History, |
| 55977 | 339 |
do_decode: Boolean, |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
340 |
explicit: Boolean, |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
341 |
start: Text.Offset, |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
342 |
text: CharSequence, |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
343 |
caret: Int, |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
344 |
extend_word: Boolean, |
| 55749 | 345 |
language_context: Completion.Language_Context): Option[Completion.Result] = |
| 31763 | 346 |
{
|
| 55977 | 347 |
def decode(s: String): String = if (do_decode) Symbol.decode(s) else s |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
348 |
val length = text.length |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
349 |
|
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
350 |
val abbrevs_result = |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
351 |
{
|
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
352 |
val reverse_in = new Library.Reverse(text.subSequence(0, caret)) |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
353 |
Scan.Parsers.parse(Scan.Parsers.literal(abbrevs_lex), reverse_in) match {
|
| 55666 | 354 |
case Scan.Parsers.Success(reverse_a, _) => |
355 |
val abbrevs = abbrevs_map.get_list(reverse_a) |
|
356 |
abbrevs match {
|
|
357 |
case Nil => None |
|
358 |
case (a, _) :: _ => |
|
359 |
val ok = |
|
| 55749 | 360 |
if (a == Completion.antiquote) language_context.antiquotes |
|
56591
1a59587f46ec
clarified abbreviations for cartouche delimiters, to work in any context;
wenzelm
parents:
56564
diff
changeset
|
361 |
else language_context.symbols || Completion.default_abbrs.exists(_._1 == a) |
| 55993 | 362 |
if (ok) Some(((a, abbrevs), caret)) |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
363 |
else None |
| 55666 | 364 |
} |
365 |
case _ => None |
|
366 |
} |
|
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
367 |
} |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
368 |
|
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
369 |
val words_result = |
| 55982 | 370 |
if (abbrevs_result.isDefined) None |
371 |
else {
|
|
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
372 |
val end = |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
373 |
if (extend_word) Completion.Word_Parsers.extend_word(text, caret) |
|
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55749
diff
changeset
|
374 |
else caret |
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
375 |
val result = |
| 55992 | 376 |
Completion.Word_Parsers.read_symbol(text.subSequence(0, end)) match {
|
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
377 |
case Some(symbol) => Some((symbol, "")) |
| 55992 | 378 |
case None => |
379 |
val word_context = |
|
380 |
end < length && Completion.Word_Parsers.is_word_char(text.charAt(end)) |
|
381 |
if (word_context) None |
|
382 |
else Completion.Word_Parsers.read_word(explicit, text.subSequence(0, end)) |
|
383 |
} |
|
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
384 |
result.map( |
| 55992 | 385 |
{
|
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
386 |
case (word, underscores) => |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
387 |
val complete_words = words_lex.completions(word) |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
388 |
val full_word = word + underscores |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
389 |
val completions = |
|
56001
2df1e7bca361
do allow replacement of words where this is appropriate, notably symbol abbrevs and keyword templates (see also 1c42ebdb3a58);
wenzelm
parents:
55996
diff
changeset
|
390 |
if (complete_words.contains(full_word) && is_keyword_template(full_word, false)) Nil |
|
55996
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
391 |
else |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
392 |
for {
|
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
393 |
complete_word <- complete_words |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
394 |
ok = |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
395 |
if (is_keyword(complete_word)) language_context.is_outer |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
396 |
else language_context.symbols |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
397 |
if ok |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
398 |
completion <- words_map.get_list(complete_word) |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
399 |
} yield (complete_word, completion) |
|
13a7d9661ffc
allow suffix of underscores for words (notably keywords), similar to semantic completion;
wenzelm
parents:
55994
diff
changeset
|
400 |
(((full_word, completions), end)) |
| 55992 | 401 |
}) |
| 53275 | 402 |
} |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
403 |
|
| 55982 | 404 |
(abbrevs_result orElse words_result) match {
|
| 55993 | 405 |
case Some(((original, completions), end)) if !completions.isEmpty => |
| 55985 | 406 |
val range = Text.Range(- original.length, 0) + end + start |
407 |
val immediate = |
|
408 |
explicit || |
|
409 |
(!Completion.Word_Parsers.is_word(original) && |
|
410 |
Character.codePointCount(original, 0, original.length) > 1) |
|
| 55993 | 411 |
val unique = completions.length == 1 |
| 55985 | 412 |
|
413 |
val items = |
|
414 |
for {
|
|
| 55993 | 415 |
(complete_word, name0) <- completions |
416 |
name1 = decode(name0) |
|
| 55985 | 417 |
if name1 != original |
418 |
(s1, s2) = |
|
419 |
space_explode(Completion.caret_indicator, name1) match {
|
|
420 |
case List(s1, s2) => (s1, s2) |
|
421 |
case _ => (name1, "") |
|
422 |
} |
|
423 |
move = - s2.length |
|
424 |
description = |
|
425 |
if (is_symbol(name0)) {
|
|
426 |
if (name0 == name1) List(name0) |
|
427 |
else List(name1, "(symbol " + quote(name0) + ")") |
|
428 |
} |
|
|
56001
2df1e7bca361
do allow replacement of words where this is appropriate, notably symbol abbrevs and keyword templates (see also 1c42ebdb3a58);
wenzelm
parents:
55996
diff
changeset
|
429 |
else if (is_keyword_template(complete_word, true)) |
| 55993 | 430 |
List(name1, "(template " + quote(complete_word) + ")") |
431 |
else if (move != 0) List(name1, "(template)") |
|
432 |
else if (is_keyword(complete_word)) List(name1, "(keyword)") |
|
| 55985 | 433 |
else List(name1) |
434 |
} |
|
435 |
yield Completion.Item(range, original, name1, description, s1 + s2, move, immediate) |
|
436 |
||
437 |
if (items.isEmpty) None |
|
| 56174 | 438 |
else |
439 |
Some(Completion.Result(range, original, unique, |
|
440 |
items.sortBy(_.name).sorted(history.ordering))) |
|
| 55985 | 441 |
|
| 55992 | 442 |
case _ => None |
| 31765 | 443 |
} |
| 31763 | 444 |
} |
445 |
} |