From ded50dfbf385ace23ff3f0279c4fd4cf6e47d256 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 17 Feb 2015 16:41:12 +0300 Subject: [PATCH] better autocompletion --- src/dlangide/ui/dsourceedit.d | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/dlangide/ui/dsourceedit.d b/src/dlangide/ui/dsourceedit.d index 1ac1aa1..11de49a 100644 --- a/src/dlangide/ui/dsourceedit.d +++ b/src/dlangide/ui/dsourceedit.d @@ -86,17 +86,36 @@ class DSourceEdit : SourceEdit { return _content.save(); } + void insertCompletion(dstring completionText) { + TextRange range; + TextPosition p = getCaretPosition; + range.start = range.end = p; + dstring lineText = content.line(p.line); + dchar prevChar = p.pos > 0 ? lineText[p.pos - 1] : 0; + dchar nextChar = p.pos < lineText.length ? lineText[p.pos] : 0; + if (isIdentMiddleChar(prevChar)) { + while(range.start.pos > 0 && isIdentMiddleChar(lineText[range.start.pos - 1])) + range.start.pos--; + if (isIdentMiddleChar(nextChar)) { + while(range.end.pos < lineText.length && isIdentMiddleChar(lineText[range.end.pos])) + range.end.pos++; + } + } + EditOperation edit = new EditOperation(EditAction.Replace, range, completionText); + _content.performOperation(edit, this); + setFocus(); + } + /// override to handle specific actions override bool handleAction(const Action a) { + import ddc.lexer.tokenizer; if (a) { switch (a.id) { case IDEActions.FileSave: save(); return true; case IDEActions.InsertCompletion: - EditOperation edit = new EditOperation(EditAction.Replace, getCaretPosition, a.label); - _content.performOperation(edit, this); - setFocus(); + insertCompletion(a.label); return true; default: break; @@ -127,6 +146,11 @@ class DSourceEdit : SourceEdit { return; } + if (suggestions.length == 1) { + insertCompletion(suggestions[0]); + return; + } + MenuItem completionPopupItems = new MenuItem(null); //Add all the suggestions. foreach(int i, dstring suggestion ; suggestions) {