Skip to content

Commit 1e079e1

Browse files
committed
2 parents d573265 + ab2e3fa commit 1e079e1

File tree

4 files changed

+70
-46
lines changed

4 files changed

+70
-46
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:highlight/highlight_core.dart';
66
import 'package:flutter/foundation.dart' show kIsWeb;
77

88
const _MIDDLE_DOT = '·';
9-
109
class EditorParams {
1110
final int tabSpaces;
1211

@@ -40,14 +39,16 @@ class CodeController extends TextEditingController {
4039
final bool webSpaceFix;
4140

4241
/// onChange callback, called whenever the content is changed
43-
final void Function(String)? onChange;
44-
final void Function(String lastToken)? onAutoComplete;
42+
final void Function(String)? onChange; // when the code change
43+
final void Function(String lastToken)? onAutoComplete; // when autocomplete keystroke is pressed
44+
final void Function()? onUnAutoComplete;// when a token delimiter is deleted
4545

4646
/* Computed members */
4747
final String languageId = _genId();
4848
final styleList = <TextStyle>[];
4949
final modifierMap = <String, CodeModifier>{};
50-
final autoCompleteKeystroke = <String>[".",":"]; //TODO: make this controllable by plugins
50+
var autoCompleteKeystroke = <String>[".",":"]; //TODO: make this controllable by plugins
51+
var autoCompleteTokenDelimiter = <String>[".",":"," "]; //TODO: make this controllable by plugins
5152
RegExp? styleRegExp;
5253

5354
CodeController({
@@ -64,7 +65,8 @@ class CodeController extends TextEditingController {
6465
],
6566
this.webSpaceFix = true,
6667
this.onChange,
67-
this.onAutoComplete
68+
this.onAutoComplete,
69+
this.onUnAutoComplete
6870
}) : super(text: text) {
6971
// PatternMap
7072
if (language != null && theme == null)
@@ -105,6 +107,10 @@ class CodeController extends TextEditingController {
105107
return;
106108
}
107109
}
110+
var char = text.characters.characterAt(selection.start-1).string;
111+
if(autoCompleteTokenDelimiter.contains(char)){
112+
onUnAutoComplete?.call();
113+
}
108114
final sel = selection;
109115
text = text.replaceRange(selection.start - 1, selection.start, "");
110116
selection = sel.copyWith(
@@ -126,8 +132,10 @@ class CodeController extends TextEditingController {
126132

127133
/// Remove the selection or last char if the selection is empty
128134
void backspace() {
129-
if (selection.start < selection.end)
135+
if (selection.start < selection.end) {
130136
removeSelection();
137+
onUnAutoComplete?.call();
138+
}
131139
else
132140
removeChar();
133141
}
@@ -143,7 +151,7 @@ class CodeController extends TextEditingController {
143151
}
144152
else if( autoCompleteKeystroke.contains(event.logicalKey.keyLabel) && event.isKeyPressed(event.logicalKey)){
145153
if(onAutoComplete != null) {
146-
onAutoComplete?.call("");
154+
onAutoComplete?.call("love");
147155
}
148156
}
149157
return KeyEventResult.ignored;

lib/editor.dart

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ class _EditorPageState extends State<EditorPage> {
199199
autoCompleteY = offset.dy + 64;
200200
});
201201
},
202+
onUnAutoComplete: (){
203+
setState(() {
204+
autoCompleteShown = false;
205+
});
206+
},
202207
);
203208

204209
codeFields.add(field);
@@ -235,13 +240,12 @@ class _EditorPageState extends State<EditorPage> {
235240
List<Widget> getAutoCompleteControls(String? a) {
236241
List<Widget> result = List.generate(autoComplete.length, (index) {
237242
var item = autoComplete[index].split("|");
238-
var type = item[0];
239-
var name = item[1];
240-
var desc = "";
241-
if(item.length > 2)
242-
{
243-
desc = item[2];
244-
}
243+
var type = "undefined", name = "name", desc = "undefined";
244+
if(item.isNotEmpty) {
245+
type = item.length > 1 ? item[0] : "undefined";
246+
name = item.length > 1 ? item[1] : item[0];
247+
desc = item.length > 2 ? item[2] : "";
248+
}
245249
var color = Colors.black12;
246250
switch(type){
247251
case "var":
@@ -250,6 +254,12 @@ class _EditorPageState extends State<EditorPage> {
250254
case "func":
251255
color = Colors.purpleAccent;
252256
break;
257+
case "type":
258+
color = Colors.orangeAccent;
259+
break;
260+
case "module":
261+
color = Colors.redAccent;
262+
break;
253263
}
254264
return InkWell(
255265
//style: TextStyle(padding: MaterialStateProperty.all(EdgeInsets.zero)),
@@ -268,8 +278,13 @@ class _EditorPageState extends State<EditorPage> {
268278
Text(
269279
name,
270280
),
271-
const Spacer(),
272-
Flexible(child:Text(desc,overflow: TextOverflow.ellipsis,))
281+
const Spacer(flex: 1,),
282+
Flexible(
283+
child:Text(desc,overflow: TextOverflow.ellipsis,textAlign: TextAlign.end,),
284+
fit: FlexFit.tight,
285+
),
286+
const SizedBox.square(dimension: 12,)
287+
273288
],
274289
),
275290
onTap: () {
@@ -295,6 +310,8 @@ class _EditorPageState extends State<EditorPage> {
295310
// Populate the file browser tree once
296311
initializeTreeView();
297312
}
313+
var tabController = TabbedViewController(tabs,);
314+
tabController.selectedIndex = selectedTab;
298315
final page = Stack(children: [
299316
Column(//direction: Axis.vertical,
300317
children: [
@@ -311,7 +328,7 @@ class _EditorPageState extends State<EditorPage> {
311328
/// Just refresh the state
312329
});
313330
},
314-
controller: TabbedViewController(tabs),
331+
controller: tabController,
315332
))
316333
: const Center(child: Text("No file opened")),
317334
),

lib/modules/module_jsplugins.dart

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,38 +215,30 @@ class JsModule extends Module {
215215
}
216216

217217
if (globalObj.hasProperty("onGetAutocomplete")) {
218-
jscore.JSValue onInitializedValue =
218+
jscore.JSValue onGetAutoComplete =
219219
globalObj.getProperty("onGetAutocomplete");
220-
jscore.JSObject onInitializedObj = onInitializedValue.toObject();
220+
jscore.JSObject funcObj = onGetAutoComplete.toObject();
221221
onAutocomplete = (String lang, String lastToken) {
222222
var result = <String>[];
223223
jscore.JSValuePointer? err;
224-
var jsResult = onInitializedObj.callAsFunction(
225-
globalObj, jscore.JSValuePointer.array([
226-
jscore.JSValue.makeString(context, "string"),
227-
jscore.JSValue.makeString(context, "love"),
228-
]),
224+
var jsResult = funcObj.callAsFunction(
225+
globalObj,
226+
jscore.JSValuePointer.array([
227+
jscore.JSValue.makeString(context, lang),
228+
jscore.JSValue.makeString(context, lastToken),
229+
]),
229230
exception: err);
230231
if (err != null && err.getValue(context).isNull == false) {
231232
debugPrint("[JSError] ${err.getValue(context).toString()}");
232233
}
233-
if(jsResult.isObject){
234+
if (jsResult.isObject) {
234235
var arr = jsResult.toObject();
235236
var props = arr.copyPropertyNames();
236-
for(var i = 0; i < props.count; i++){
237+
for (var i = 0; i < props.count; i++) {
237238
var name = props.propertyNameArrayGetNameAtIndex(i);
238-
if(lastToken != "" && name == lastToken){
239-
var inner = arr.getProperty(name).toObject();
240-
var props2 = inner.copyPropertyNames();
241-
for(var k = 0; k < props2.count; k++) {
242-
result.add(props2.propertyNameArrayGetNameAtIndex(k));
243-
}
244-
}
245-
if(lastToken == ""){
246-
result.add(name);
247-
}
239+
result.add(name);
248240
}
249-
}else{
241+
} else {
250242
debugPrint("It's not an object");
251243
}
252244
return result;
@@ -256,9 +248,9 @@ class JsModule extends Module {
256248

257249
@override
258250
List<String> onAutoComplete(String language, String lastToken) {
259-
if(onAutocomplete != null) {
251+
if (onAutocomplete != null) {
260252
List<String>? list = onAutocomplete?.call(language, lastToken);
261-
if(list != null){
253+
if (list != null) {
262254
return list;
263255
}
264256
}

lib/util/custom_code_box.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,41 @@ class InnerField extends StatelessWidget {
122122
Function(String filePath, String source)? onChange;
123123
Function(Offset offset)? setCursorOffset;
124124
Function(String lastToken)? onAutoComplete;
125+
Function()? onUnAutoComplete;
125126
final String filePath;
126127
late CodeField codeField;
127128

128129

129-
InnerField({Key? key, required this.language, required this.theme, required this.source, required this.filePath, this.onChange, this.onAutoComplete, this.setCursorOffset})
130+
InnerField({Key? key, required this.language, required this.theme, required this.source, required this.filePath, this.onChange, this.onAutoComplete, this.onUnAutoComplete, this.setCursorOffset})
130131
: super(key: key){
131132
codeController = CodeController(
132133
text: source,
133-
params: EditorParams(tabSpaces: 4),
134+
params: const EditorParams(tabSpaces: 4),
134135
patternMap: {
135-
r"\B#[a-zA-Z0-9]+\b": TextStyle(color: Colors.red),
136-
r"\B@[a-zA-Z0-9]+\b": TextStyle(
136+
r"\B#[a-zA-Z0-9]+\b": const TextStyle(color: Colors.red),
137+
r"\B@[a-zA-Z0-9]+\b": const TextStyle(
137138
fontWeight: FontWeight.w800,
138139
color: Colors.blue,
139140
),
140141
r"\B![a-zA-Z0-9]+\b":
141-
TextStyle(color: Colors.yellow, fontStyle: FontStyle.italic),
142+
const TextStyle(color: Colors.yellow, fontStyle: FontStyle.italic),
142143
},
143144
stringMap: {
144-
"bev": TextStyle(color: Colors.indigo),
145+
"bev": const TextStyle(color: Colors.indigo),
145146
},
146147
language: allLanguages[language],
147148
theme: theme,
148149
onAutoComplete: (String lastToken){
149150
if(onAutoComplete != null) {
150151
onAutoComplete!.call(lastToken);
151152
}
152-
});
153+
},
154+
onUnAutoComplete: (){
155+
if(onUnAutoComplete != null) {
156+
onUnAutoComplete!.call();
157+
}
158+
},
159+
);
153160
codeField = CodeField(
154161
onCursorPosChanged: setCursorOffset,
155162
controller: codeController,

0 commit comments

Comments
 (0)