private LinePosition GetPreceedingWordSegment(ICSharpCode.AvalonEdit.Document.IDocument document, ISegment completionSegment) { string line = ""; int pos = completionSegment.EndOffset - 1; var loc = document.GetLocation(pos); Log.Debug("{class} {method} pos:{position}", "DaxCompletionData", "GetPreceedingWordSegment", pos); var docLine = document.GetLineByOffset(pos); //line = textArea.Document.GetText(docLine.Offset, loc.Column); line = document.GetText(docLine.Offset, docLine.Length); Log.Verbose("{class} {method} {message}", "DaxCompletionData", "GetPreceedingWordSegment", "line: " + line); var daxState = DaxLineParser.ParseLine(line, loc.Column, 0); //TODO - look ahead to see if we have a table/column/function end character that we should replace upto return(DaxLineParser.GetPreceedingWordSegment(docLine.Offset, loc.Column, line, daxState)); }
public void CompleteInternal(ICSharpCode.AvalonEdit.Document.IDocument document, ISegment completionSegment, EventArgs insertionRequestEventArgs) { Log.Debug("{class} {method} {start}-{end}({length})", "DaxCompletionData", "Complete", completionSegment.Offset, completionSegment.EndOffset, completionSegment.Length); try { // walk back to start of word var newSegment = GetPreceedingWordSegment(document, completionSegment); var replaceOffset = newSegment.Offset; var replaceLength = newSegment.Length; var funcParamStart = Text.IndexOf("«", StringComparison.OrdinalIgnoreCase); string insertionText = funcParamStart > 0 ? Text.Substring(0, funcParamStart) : Text; if (insertionRequestEventArgs is TextCompositionEventArgs args) { // if the insertion char is the same as the last char in the // insertion text then trim it off var insertionChar = args.Text; if (insertionText.EndsWith(insertionChar, StringComparison.Ordinal)) { insertionText = insertionText.TrimEnd(insertionChar[0]); } } if (completionSegment.EndOffset <= document.TextLength - 1) { var lastCompletionChar = insertionText[insertionText.Length - 1]; var lastDocumentChar = document.GetCharAt(completionSegment.EndOffset); Log.Debug("{class} {method} {lastCompletionChar} vs {lastDocumentChar} off: {offset} len:{length}", "DaxCompletionData", "Complete", lastCompletionChar, lastDocumentChar, newSegment.Offset, newSegment.Length); if (lastCompletionChar == lastDocumentChar) { replaceLength++; } } document.Replace(newSegment.Offset, newSegment.Length, insertionText); _insightProvider.ShowInsight(insertionText); } catch (Exception ex) { Log.Fatal(ex, "{class} {method} Error inserting code completion data {message}", "DaxCompletionData", "CompleteInternal", ex.Message); } }