Skip to content

Commit cedcde6

Browse files
committed
feat: Add acceptSuggestion API to manually accept LSP suggestions and update version to 8.1.1
1 parent bd09b55 commit cedcde6

6 files changed

Lines changed: 52 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,7 @@ This release establishes **CodeForge** as a powerful, production-ready code edit
304304
- Updated README.
305305

306306
## 8.1.0
307-
- ENHANCEMENT: Enhanced RTL support
307+
- ENHANCEMENT: Enhanced RTL support
308+
309+
## 8.1.1
310+
- FEATURE: added a public API called `acceptSuggestion` in the controller to manually accept LSP suggestions.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
> code_forge does **not** support Flutter web, as it relies on `dart:io` for core functionality. Use [code_forge_web](https://pub.dev/packages/code_forge_web) for web support.
3939
4040

41-
### What's new in 8.1.0
42-
- ENHANCEMENT: Enhanced RTL support
41+
### What's new in 8.1.1
42+
- FEATURE: added a public API called `acceptSuggestion` in the controller to manually accept LSP suggestions.
4343

4444
## Why CodeForge?
4545
**Feature demos:** [CodeForge Features Showcase](https://heckmon.github.io/code_forge_demo/)
@@ -114,7 +114,7 @@ Add CodeForge to your `pubspec.yaml`:
114114

115115
```yaml
116116
dependencies:
117-
code_forge: ^8.1.0
117+
code_forge: ^8.1.1
118118
```
119119
120120
Then run:

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class _MyAppState extends State<MyApp> {
2626
Future<LspConfig> getLsp() async {
2727
final absWorkspacePath = p.join(Directory.current.path, "lib");
2828
final data = await LspStdioConfig.start(
29-
executable: "/home/athul/flutter/flutter/bin//dart",
29+
executable: "dart",
3030
args: ["language-server", "--protocol=lsp"],
3131
workspacePath: absWorkspacePath,
3232
languageId: "dart",

lib/code_forge/code_area.dart

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,30 +3302,8 @@ class _CodeForgeState extends State<CodeForge> with TickerProviderStateMixin {
33023302
}
33033303

33043304
void _acceptSuggestion() {
3305-
final suggestions = _suggestionNotifier.value;
3306-
if (suggestions == null || suggestions.isEmpty) return;
3307-
3308-
final selected = _isMobileSuggActive
3309-
? suggestions[_controller.currentlySelectedSuggestion!]
3310-
: suggestions[_sugSelIndex];
3311-
String insertText = '';
3312-
3313-
if (selected is LspCompletion) {
3314-
insertText = selected.label;
3315-
} else if (selected is Map) {
3316-
insertText = selected['insertText'] ?? selected['label'] ?? '';
3317-
} else if (selected is String) {
3318-
insertText = selected;
3319-
}
3320-
3321-
if (insertText.isNotEmpty) {
3322-
_controller.insertAtCurrentCursor(insertText, replaceTypedChar: true);
3323-
}
3324-
3325-
_suggestionNotifier.value = null;
3326-
_isMobileSuggActive
3327-
? _controller.currentlySelectedSuggestion = 0
3328-
: _sugSelIndex = 0;
3305+
_controller.acceptSuggestion(selectedIndex: _sugSelIndex);
3306+
_sugSelIndex = 0;
33293307
}
33303308

33313309
void _acceptGhostText() {

lib/code_forge/controller.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,47 @@ class CodeForgeController implements DeltaTextInputClient {
444444
codeActionsNotifier.value = null;
445445
}
446446

447+
/// Accepts the currently selected suggestion and inserts it at the cursor position.
448+
///
449+
/// For mobile devices, uses [currentlySelectedSuggestion] to determine which
450+
/// suggestion to accept. For desktop/non-mobile, uses the provided [selectedIndex].
451+
///
452+
/// The method handles different suggestion types:
453+
/// - [LspCompletion]: Uses the label property
454+
/// - [Map]: Uses 'insertText' or 'label' key
455+
/// - [String]: Uses the string directly
456+
///
457+
/// After accepting, clears the suggestions and resets the selection index.
458+
///
459+
/// Parameters:
460+
/// - [selectedIndex]: The index of the selected suggestion for desktop/non-mobile.
461+
/// Defaults to 0 if not provided.
462+
void acceptSuggestion({int selectedIndex = 0}) {
463+
final suggestions = suggestionsNotifier.value;
464+
if (suggestions == null || suggestions.isEmpty) return;
465+
466+
final isMobile = Platform.isAndroid || Platform.isIOS;
467+
final selected = isMobile
468+
? suggestions[currentlySelectedSuggestion!]
469+
: suggestions[selectedIndex];
470+
String insertText = '';
471+
472+
if (selected is LspCompletion) {
473+
insertText = selected.label;
474+
} else if (selected is Map) {
475+
insertText = selected['insertText'] ?? selected['label'] ?? '';
476+
} else if (selected is String) {
477+
insertText = selected;
478+
}
479+
480+
if (insertText.isNotEmpty) {
481+
insertAtCurrentCursor(insertText, replaceTypedChar: true);
482+
}
483+
484+
suggestionsNotifier.value = null;
485+
currentlySelectedSuggestion = 0;
486+
}
487+
447488
/// Adds a line decoration to the editor.
448489
///
449490
/// Line decorations can highlight code ranges with background colors,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: code_forge
22
description: "A sophisticated code editor package with AI completion, LSP support, syntax highlighting, and advanced editing capabilities."
3-
version: 8.1.0
3+
version: 8.1.1
44
homepage: https://github.com/heckmon/code_forge
55

66
environment:

0 commit comments

Comments
 (0)