Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Enhance autocomplete, support case-insensitive #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.15.0"
path:
dependency: transitive
description:
Expand All @@ -92,7 +92,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.4.0"
version: "0.6.0"
re_highlight:
dependency: "direct main"
description:
Expand Down Expand Up @@ -122,14 +122,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.3.0"
sdks:
dart: ">=3.2.0-194.0.dev <4.0.0"
dart: ">=3.3.0-0 <4.0.0"
flutter: ">=1.17.0"
41 changes: 22 additions & 19 deletions lib/src/code_autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ part of re_editor;
abstract class CodePrompt {

const CodePrompt({
required this.word
required this.word,
this.caseSensitive = true,
});

/// Whether the prompt is case sensitive.
///
/// e.g.
/// - when false, user input is 'str', the prompt word 'String' will be displayed to user.
/// - when true, user input is 'str', the prompt word 'String' will not be displayed to user.
/// default to true
final bool caseSensitive;

/// Content associated with user input.
///
Expand All @@ -21,25 +30,26 @@ abstract class CodePrompt {
CodeAutocompleteResult get autocomplete;

/// Check whether the input meets this prompt condition.
bool match(String input);
bool match(String input) {
return word != input &&
(caseSensitive
? word.startsWith(input)
: word.toLowerCase().startsWith(input.toLowerCase()));
}

}

/// The keyword autocomplate prompt. such as 'return', 'class', 'new' and so on.
class CodeKeywordPrompt extends CodePrompt {

const CodeKeywordPrompt({
required super.word
required super.word,
super.caseSensitive = true,
});

@override
CodeAutocompleteResult get autocomplete => CodeAutocompleteResult.fromWord(word);

@override
bool match(String input) {
return word != input && word.startsWith(input);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
Expand All @@ -62,6 +72,7 @@ class CodeFieldPrompt extends CodePrompt {
const CodeFieldPrompt({
required super.word,
required this.type,
super.caseSensitive = true,
this.customAutocomplete,
});

Expand All @@ -74,11 +85,6 @@ class CodeFieldPrompt extends CodePrompt {
@override
CodeAutocompleteResult get autocomplete => customAutocomplete ?? CodeAutocompleteResult.fromWord(word);

@override
bool match(String input) {
return word != input && word.startsWith(input);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
Expand All @@ -98,6 +104,7 @@ class CodeFunctionPrompt extends CodePrompt {

const CodeFunctionPrompt({
required super.word,
super.caseSensitive = true,
required this.type,
this.parameters = const {},
this.optionalParameters = const {},
Expand All @@ -119,11 +126,6 @@ class CodeFunctionPrompt extends CodePrompt {
@override
CodeAutocompleteResult get autocomplete => customAutocomplete ?? CodeAutocompleteResult.fromWord('$word(${parameters.keys.join(', ')})');

@override
bool match(String input) {
return word != input && word.startsWith(input);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
Expand Down Expand Up @@ -201,7 +203,8 @@ class CodeAutocompleteEditingValue {
}

CodeAutocompleteResult get autocomplete {
final CodeAutocompleteResult result = prompts[index].autocomplete;
final selectedPrompt = prompts[index];
final CodeAutocompleteResult result = selectedPrompt.autocomplete;
if (result.word.isEmpty) {
return result;
}
Expand Down