Skip to content

Commit 8b34d77

Browse files
committed
Autocomplete almost done
1 parent 2c5d721 commit 8b34d77

File tree

2 files changed

+93
-59
lines changed

2 files changed

+93
-59
lines changed

lib/contrib/code_text_field-1.0.2m/lib/src/code_controller.dart

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CodeController extends TextEditingController {
4848
final styleList = <TextStyle>[];
4949
final modifierMap = <String, CodeModifier>{};
5050
var autoCompleteKeystroke = <String>[".",":"]; //TODO: make this controllable by plugins
51-
var autoCompleteTokenDelimiter = <String>[".",":"," "]; //TODO: make this controllable by plugins
51+
var autoCompleteTokenDelimiter = <String>[".",":"," ","\n"]; //TODO: make this controllable by plugins
5252
RegExp? styleRegExp;
5353

5454
CodeController({
@@ -136,8 +136,9 @@ class CodeController extends TextEditingController {
136136
removeSelection();
137137
onUnAutoComplete?.call();
138138
}
139-
else
139+
else {
140140
removeChar();
141+
}
141142
}
142143

143144
KeyEventResult onKey(RawKeyEvent event) {
@@ -151,7 +152,15 @@ class CodeController extends TextEditingController {
151152
}
152153
else if( autoCompleteKeystroke.contains(event.logicalKey.keyLabel) && event.isKeyPressed(event.logicalKey)){
153154
if(onAutoComplete != null) {
154-
onAutoComplete?.call("love");
155+
int i = selection.start;
156+
while(i > 0){
157+
i--;
158+
var char = rawText.characters.characterAt(i).string;
159+
if(autoCompleteTokenDelimiter.contains(char)){
160+
break;
161+
}
162+
}
163+
onAutoComplete?.call(rawText.substring(i+1,selection.start));
155164
}
156165
}
157166
return KeyEventResult.ignored;
@@ -208,11 +217,13 @@ class CodeController extends TextEditingController {
208217
}
209218
}
210219
// Now fix the textfield for web
211-
if (_webSpaceFix)
220+
if (_webSpaceFix) {
212221
newValue = newValue.copyWith(text: _spacesToMiddleDots(newValue.text));
213-
if (onChange != null)
222+
}
223+
if (onChange != null) {
214224
onChange!(
215225
_webSpaceFix ? _middleDotsToSpaces(newValue.text) : newValue.text);
226+
}
216227
super.value = newValue;
217228
}
218229

@@ -258,8 +269,9 @@ class CodeController extends TextEditingController {
258269
if (val != null) {
259270
if (_webSpaceFix) val = _spacesToMiddleDots(val);
260271
var child = TextSpan(text: val, style: theme?[node.className]);
261-
if (styleRegExp != null)
272+
if (styleRegExp != null) {
262273
child = _processPatterns(val, theme?[node.className]);
274+
}
263275
currentSpans.add(child);
264276
} else if (nodeChildren != null) {
265277
List<TextSpan> tmp = [];
@@ -278,7 +290,11 @@ class CodeController extends TextEditingController {
278290
}
279291
}
280292

281-
if (nodes != null) for (var node in nodes) _traverse(node);
293+
if (nodes != null) {
294+
for (var node in nodes) {
295+
_traverse(node);
296+
}
297+
}
282298
return TextSpan(style: style, children: children);
283299
}
284300

@@ -289,7 +305,7 @@ class CodeController extends TextEditingController {
289305
final patternList = <String>[];
290306
if (_webSpaceFix) {
291307
patternList.add("(" + _MIDDLE_DOT + ")");
292-
styleList.add(TextStyle(color: Colors.transparent));
308+
styleList.add(const TextStyle(color: Colors.transparent));
293309
}
294310
if (stringMap != null) {
295311
patternList.addAll(stringMap!.keys.map((e) => r'(\b' + e + r'\b)'));
@@ -302,11 +318,12 @@ class CodeController extends TextEditingController {
302318
styleRegExp = RegExp(patternList.join('|'), multiLine: true);
303319

304320
// Return parsing
305-
if (language != null)
321+
if (language != null) {
306322
return _processLanguage(text, style);
307-
else if (styleRegExp != null)
323+
} else if (styleRegExp != null) {
308324
return _processPatterns(text, style);
309-
else
325+
} else {
310326
return TextSpan(text: text, style: style);
327+
}
311328
}
312329
}

lib/editor.dart

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ class _EditorPageState extends State<EditorPage> {
186186
},
187187
onAutoComplete: (String lastToken) {
188188
autoComplete = [];
189-
for(var module in ModulesManager.modules){
190-
autoComplete.addAll(module.onAutoComplete(language, lastToken));
189+
for (var module in ModulesManager.modules) {
190+
autoComplete.addAll(module.onAutoComplete(language, lastToken));
191191
}
192192
setState(() {
193193
autoCompleteShown = true;
@@ -199,7 +199,7 @@ class _EditorPageState extends State<EditorPage> {
199199
autoCompleteY = offset.dy + 64;
200200
});
201201
},
202-
onUnAutoComplete: (){
202+
onUnAutoComplete: () {
203203
setState(() {
204204
autoCompleteShown = false;
205205
});
@@ -242,17 +242,17 @@ class _EditorPageState extends State<EditorPage> {
242242
List<Widget> result = List.generate(autoComplete.length, (index) {
243243
var item = autoComplete[index].split("|");
244244
var type = "undefined", name = "name", desc = "undefined";
245-
if(item.isNotEmpty) {
245+
if (item.isNotEmpty) {
246246
type = item.length > 1 ? item[0] : "undefined";
247247
name = item.length > 1 ? item[1] : item[0];
248248
desc = item.length > 2 ? item[2] : "";
249249
}
250250
var color = Colors.black12;
251-
switch(type){
251+
switch (type) {
252252
case "var":
253253
color = Colors.blueAccent;
254254
break;
255-
case "func":
255+
case "function":
256256
color = Colors.purpleAccent;
257257
break;
258258
case "type":
@@ -262,43 +262,56 @@ class _EditorPageState extends State<EditorPage> {
262262
color = Colors.redAccent;
263263
break;
264264
}
265-
return InkWell(
266-
//style: TextStyle(padding: MaterialStateProperty.all(EdgeInsets.zero)),
267-
child: Row(
268-
children: [
269-
Container(
270-
color: color,
271-
child: Text(
272-
type.characters.first,
273-
textAlign: TextAlign.center,
274-
),
275-
padding: const EdgeInsets.all(8.0),
276-
margin: const EdgeInsets.only(right: 8.0),
277-
width: 32,
278-
),
279-
Text(
280-
name,
265+
return Tooltip(
266+
verticalOffset: 200,
267+
message: desc,
268+
child: InkWell(
269+
//style: TextStyle(padding: MaterialStateProperty.all(EdgeInsets.zero)),
270+
child: Row(
271+
children: [
272+
Container(
273+
color: color,
274+
child: Text(
275+
type.characters.first,
276+
textAlign: TextAlign.center,
277+
),
278+
padding: const EdgeInsets.all(8.0),
279+
margin: const EdgeInsets.only(right: 8.0),
280+
width: 32,
281+
),
282+
Text(
283+
name,
284+
),
285+
const Spacer(
286+
flex: 1,
287+
),
288+
Flexible(
289+
child: Text(
290+
desc,
291+
overflow: TextOverflow.ellipsis,
292+
textAlign: TextAlign.end,
293+
),
294+
fit: FlexFit.tight,
295+
),
296+
const SizedBox.square(
297+
dimension: 12,
298+
)
299+
],
281300
),
282-
const Spacer(flex: 1,),
283-
Flexible(
284-
child:Text(desc,overflow: TextOverflow.ellipsis,textAlign: TextAlign.end,),
285-
fit: FlexFit.tight,
286-
),
287-
const SizedBox.square(dimension: 12,)
288-
289-
],
290-
),
291-
onTap: () {
292-
setState(() {
293-
autoCompleteShown = false;
294-
if(selectedTab == null || selectedTab!< 0) return;
295-
var currentTab = tabs[selectedTab!];
296-
var currentField = ((currentTab.content as SingleChildScrollView).child as Container).child as InnerField;
297-
var controller = currentField.codeController;
298-
controller.insertStr(name);
299-
});
300-
},
301-
);
301+
onTap: () {
302+
setState(() {
303+
autoCompleteShown = false;
304+
if (selectedTab == null || selectedTab! < 0) return;
305+
var currentTab = tabs[selectedTab!];
306+
var currentField =
307+
((currentTab.content as SingleChildScrollView).child
308+
as Container)
309+
.child as InnerField;
310+
var controller = currentField.codeController;
311+
controller.insertStr(name);
312+
});
313+
},
314+
));
302315
});
303316

304317
return result;
@@ -311,7 +324,9 @@ class _EditorPageState extends State<EditorPage> {
311324
// Populate the file browser tree once
312325
initializeTreeView();
313326
}
314-
var tabController = TabbedViewController(tabs,);
327+
var tabController = TabbedViewController(
328+
tabs,
329+
);
315330
tabController.selectedIndex = selectedTab;
316331
final page = Stack(children: [
317332
Column(//direction: Axis.vertical,
@@ -321,7 +336,7 @@ class _EditorPageState extends State<EditorPage> {
321336
? TabbedViewTheme(
322337
data: getTabTheme(),
323338
child: TabbedView(
324-
onTabSelection: (int? selection){
339+
onTabSelection: (int? selection) {
325340
selectedTab = selection ?? -1;
326341
},
327342
onTabClose: (tabIndex, tabData) {
@@ -341,7 +356,11 @@ class _EditorPageState extends State<EditorPage> {
341356
width: 512,
342357
child: ClipRRect(
343358
child: Container(
344-
constraints: const BoxConstraints(minWidth: 256,maxWidth: 600,minHeight: 16,maxHeight: 300),
359+
constraints: const BoxConstraints(
360+
minWidth: 256,
361+
maxWidth: 600,
362+
minHeight: 16,
363+
maxHeight: 300),
345364
clipBehavior: Clip.none,
346365
color: ThemeManager.getThemeSchemeColor("foreground"),
347366
child: Material(
@@ -368,9 +387,7 @@ class _EditorPageState extends State<EditorPage> {
368387
icon: const Icon(Icons.close),
369388
tooltip: "Close Project",
370389
),
371-
IconButton(
372-
onPressed: () => {},
373-
icon: const Icon(Icons.more_horiz)),
390+
IconButton(onPressed: () => {}, icon: const Icon(Icons.more_horiz)),
374391
const SizedBox(width: 16.0),
375392
],
376393
),

0 commit comments

Comments
 (0)