public static string GetWordAtIndex(this ITextDocument document, int index) { var result = string.Empty; if (index >= 0 && document.TextLength > index) { var start = index; var currentChar = document.GetCharAt(index); var prevChar = '\0'; if (index > 0) { prevChar = document.GetCharAt(index - 1); } var charClass = TextUtilities.GetCharacterClass(currentChar); if (charClass != CharacterClass.LineTerminator && prevChar != ' ' && TextUtilities.GetCharacterClass(prevChar) != CharacterClass.LineTerminator) { start = TextUtilities.GetNextCaretPosition(document, index, LogicalDirection.Backward, CaretPositioningMode.WordStart); } var end = TextUtilities.GetNextCaretPosition(document, start, LogicalDirection.Forward, CaretPositioningMode.WordBorder); if (start != -1 && end != -1) { var word = document.GetText(start, end - start).Trim(); if (word.IsSymbol()) { result = word; } } } return(result); }
static void ConvertVerbatimStringToNormal(ITextDocument textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt(endOffset); if (ch == '"' && (endOffset + 1 < textEditorData.Length && textEditorData.GetCharAt(endOffset + 1) == '"')) { endOffset += 2; continue; } if (ch == '"') { break; } endOffset++; } var plainText = CSharpTextPasteHandler.TextPasteUtils.VerbatimStringStrategy.Decode(textEditorData.GetTextAt(offset, endOffset - offset)); var newText = CSharpTextPasteHandler.TextPasteUtils.StringLiteralPasteStrategy.Instance.Encode(plainText); textEditorData.ReplaceText(offset, endOffset - offset, newText); }
static void ConvertNormalToVerbatimString(ITextDocument textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt(endOffset); if (ch == '\\') { if (endOffset + 1 < textEditorData.Length && NewLine.IsNewLine(textEditorData.GetCharAt(endOffset + 1))) { return; } endOffset += 2; continue; } if (ch == '"') { break; } if (NewLine.IsNewLine(ch)) { return; } endOffset++; } if (offset > endOffset || endOffset == textEditorData.Length) { return; } var plainText = CSharpTextPasteHandler.TextPasteUtils.StringLiteralPasteStrategy.Instance.Decode(textEditorData.GetTextAt(offset, endOffset - offset)); var newText = CSharpTextPasteHandler.TextPasteUtils.VerbatimStringStrategy.Encode(plainText); textEditorData.ReplaceText(offset, endOffset - offset, newText); }
public static ISegment GetToken(this ITextDocument document, int offset) { var result = string.Empty; if (offset >= 0 && document.TextLength > offset) { var start = offset; var currentChar = document.GetCharAt(offset); var prevChar = '\0'; if (offset > 0) { prevChar = document.GetCharAt(offset - 1); } var charClass = AvaloniaEdit.Document.TextUtilities.GetCharacterClass(currentChar); if (charClass != AvaloniaEdit.Document.CharacterClass.LineTerminator && prevChar != ' ' && AvaloniaEdit.Document.TextUtilities.GetCharacterClass(prevChar) != AvaloniaEdit.Document.CharacterClass.LineTerminator) { start = TextUtilities.GetNextCaretPosition(document, offset, LogicalDirection.Backward, CaretPositioningMode.WordStart); } var end = TextUtilities.GetNextCaretPosition(document, start, LogicalDirection.Forward, CaretPositioningMode.WordBorder); if (start >= 0 && end >= 0 && end < document.TextLength && end >= start) { return(new SimpleSegment(start, end - start)); } } return(null); }
public static int GetIntellisenseStartPosition(this ITextDocument textSource, int offset, Predicate <char> isValidChar) { while (true) { var currentChar = textSource.GetCharAt(offset - 1); if (!isValidChar(currentChar) || offset < 0) { break; } offset--; } return(offset); }
public static (int index, char character) GetLastNonWhiteSpaceCharBefore(this ITextDocument textSource, int index, int minIndex = 0) { while (index >= minIndex) { var currentChar = textSource.GetCharAt(index); if (!char.IsWhiteSpace(currentChar)) { return(index, currentChar); } index--; } return(-1, '\0'); }
public static ISegment GetWhitespaceBefore(this ITextDocument textSource, int offset) { if (textSource == null) { throw new ArgumentNullException(nameof(textSource)); } int pos; for (pos = offset - 1; pos >= 0; pos--) { char c = textSource.GetCharAt(pos); if (c != ' ' && c != '\t') { break; } } pos++; // go back the one character that isn't whitespace return(new SimpleSegment(pos, offset - pos)); }
public static ISegment GetWhitespaceAfter(this ITextDocument textSource, int offset) { if (textSource == null) { throw new ArgumentNullException(nameof(textSource)); } int pos; for (pos = offset; pos < textSource.TextLength; pos++) { char c = textSource.GetCharAt(pos); if (c != ' ' && c != '\t') { break; } } return(new SimpleSegment(offset, pos - offset)); }
private void OpenBracket(IEditor editor, ITextDocument document, string text) { if (text[0].IsOpenBracketChar() && editor.CaretOffset <= document.TextLength && editor.CaretOffset > 0) { var nextChar = ' '; if (editor.CaretOffset != document.TextLength) { nextChar = document.GetCharAt(editor.CaretOffset); } var location = document.GetLocation(editor.CaretOffset); if (char.IsWhiteSpace(nextChar) || nextChar.IsCloseBracketChar()) { if (text[0] == '{') { var offset = editor.CaretOffset; document.Insert(editor.CaretOffset, " " + text[0].GetCloseBracketChar().ToString() + " "); if (IndentationStrategy != null) { editor.IndentLine(editor.Line); } editor.CaretOffset = offset + 1; } else { var offset = editor.CaretOffset; document.Insert(editor.CaretOffset, text[0].GetCloseBracketChar().ToString()); editor.CaretOffset = offset; } } } }
public static (int index, char character) GetLastCharMatching(this ITextDocument textSource, Predicate <char> predicate, int startIndex, int minIndex = 0, int skip = 0) { while (startIndex >= minIndex) { var currentChar = textSource.GetCharAt(startIndex); if (predicate(currentChar)) { if (skip == 0) { return(startIndex, currentChar); } else { skip--; } } startIndex--; } return(-1, '\0'); }
private void CloseBracket(IEditor editor, ITextDocument document, string text) { if (text[0].IsCloseBracketChar() && editor.CaretOffset < document.TextLength && editor.CaretOffset > 0) { var offset = editor.CaretOffset; while (offset < document.TextLength) { var currentChar = document.GetCharAt(offset); if (currentChar == text[0]) { document.Replace(offset, 1, string.Empty); break; } else if (!currentChar.IsWhiteSpace()) { break; } offset++; } } }
public static int GetNextCaretPosition(ITextDocument textSource, int offset, LogicalDirection direction, CaretPositioningMode mode) { if (textSource == null) { throw new ArgumentNullException(nameof(textSource)); } switch (mode) { case CaretPositioningMode.Normal: case CaretPositioningMode.EveryCodepoint: case CaretPositioningMode.WordBorder: case CaretPositioningMode.WordBorderOrSymbol: case CaretPositioningMode.WordStart: case CaretPositioningMode.WordStartOrSymbol: break; // OK default: throw new ArgumentException("Unsupported CaretPositioningMode: " + mode, nameof(mode)); } if (direction != LogicalDirection.Backward && direction != LogicalDirection.Forward) { throw new ArgumentException("Invalid LogicalDirection: " + direction, nameof(direction)); } int textLength = textSource.TextLength; if (textLength <= 0) { // empty document? has a normal caret position at 0, though no word borders if (IsNormal(mode)) { if (offset > 0 && direction == LogicalDirection.Backward) { return(0); } if (offset < 0 && direction == LogicalDirection.Forward) { return(0); } } return(-1); } while (true) { int nextPos = (direction == LogicalDirection.Backward) ? offset - 1 : offset + 1; // return -1 if there is no further caret position in the text source // we also need this to handle offset values outside the valid range if (nextPos < 0 || nextPos > textLength) { return(-1); } // check if we've run against the textSource borders. // a 'textSource' usually isn't the whole document, but a single VisualLineElement. if (nextPos == 0) { // at the document start, there's only a word border // if the first character is not whitespace if (IsNormal(mode) || !char.IsWhiteSpace(textSource.GetCharAt(0))) { return(nextPos); } } else if (nextPos == textLength) { // at the document end, there's never a word start if (mode != CaretPositioningMode.WordStart && mode != CaretPositioningMode.WordStartOrSymbol) { // at the document end, there's only a word border // if the last character is not whitespace if (IsNormal(mode) || !char.IsWhiteSpace(textSource.GetCharAt(textLength - 1))) { return(nextPos); } } } else { char charBefore = textSource.GetCharAt(nextPos - 1); char charAfter = textSource.GetCharAt(nextPos); // Don't stop in the middle of a surrogate pair if (!char.IsSurrogatePair(charBefore, charAfter)) { CharacterClass classBefore = GetCharacterClass(charBefore); CharacterClass classAfter = GetCharacterClass(charAfter); // get correct class for characters outside BMP: if (char.IsLowSurrogate(charBefore) && nextPos >= 2) { classBefore = GetCharacterClass(textSource.GetCharAt(nextPos - 2), charBefore); } if (char.IsHighSurrogate(charAfter) && nextPos + 1 < textLength) { classAfter = GetCharacterClass(charAfter, textSource.GetCharAt(nextPos + 1)); } if (StopBetweenCharacters(mode, classBefore, classAfter)) { return(nextPos); } } } // we'll have to continue searching... offset = nextPos; } }
static void ConvertVerbatimStringToNormal (ITextDocument textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt (endOffset); if (ch == '"' && (endOffset + 1 < textEditorData.Length && textEditorData.GetCharAt (endOffset + 1) == '"')) { endOffset += 2; continue; } if (ch == '"') { break; } endOffset++; } var plainText = TextPasteUtils.VerbatimStringStrategy.Decode (textEditorData.GetTextAt (offset, endOffset - offset)); var newText = TextPasteUtils.StringLiteralPasteStrategy.Instance.Encode (plainText); textEditorData.ReplaceText (offset, endOffset - offset, newText); }
static void ConvertNormalToVerbatimString (ITextDocument textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt (endOffset); if (ch == '\\') { if (endOffset + 1 < textEditorData.Length && NewLine.IsNewLine (textEditorData.GetCharAt (endOffset + 1))) return; endOffset += 2; continue; } if (ch == '"') break; if (NewLine.IsNewLine (ch)) return; endOffset++; } if (offset > endOffset || endOffset == textEditorData.Length) return; var plainText = TextPasteUtils.StringLiteralPasteStrategy.Instance.Decode (textEditorData.GetTextAt (offset, endOffset - offset)); var newText = TextPasteUtils.VerbatimStringStrategy.Encode (plainText); textEditorData.ReplaceText (offset, endOffset - offset, newText); }
static void ResortImports(List <ImportStatement> importsToSort, ITextDocument editor, List <DAttribute> attributesNotToWrite, bool separatePackageRoots) { if (importsToSort.Count < 2) { return; } int firstOffset = int.MaxValue; string indent = ""; // Remove all of them from the document; Memorize where the first import was for (int i = importsToSort.Count - 1; i >= 0; i--) { var ss = importsToSort[i]; var ssLocation = ss.Location; var ssEndLocation = ss.EndLocation; DAttribute attr; if (ss.Attributes != null && ss.Attributes.Length > 0) { attr = ss.Attributes.FirstOrDefault((e) => !attributesNotToWrite.Contains(e)); if (attr != null && attr.Location < ssLocation) { ssLocation = attr.Location; } attr = ss.Attributes.LastOrDefault((e) => !attributesNotToWrite.Contains(e)); if (attr != null && attr.EndLocation > ssEndLocation) { ssEndLocation = attr.EndLocation; } } var l1 = editor.LocationToOffset(ssLocation.Line, ssLocation.Column); var l2 = editor.LocationToOffset(ssEndLocation.Line, ssEndLocation.Column); var n = editor.Length - 1; // Remove indents and trailing semicolon. for (char c; l1 > 0 && ((c = editor.GetCharAt(l1 - 1)) == ' ' || c == '\t'); l1--) { ; } for (char c; l2 < n && ((c = editor.GetCharAt(l2 + 1)) == ' ' || c == '\t' || c == ';'); l2++) { ; } for (char c; l2 < n && ((c = editor.GetCharAt(l2 + 1)) == '\n' || c == '\r'); l2++) { ; } l1 = Math.Max(0, l1); l2 = Math.Max(0, l2); firstOffset = Math.Min(l1, firstOffset); indent = editor.GetLineIndent(editor.OffsetToLineNumber(firstOffset)); editor.Remove(l1, l2 - l1); } // Sort importsToSort.Sort(new ImportComparer()); // Write all imports beneath each other. var eol = editor.EolMarker; var sb = new StringBuilder(); ITypeDeclaration prevId = null; foreach (var i in importsToSort) { sb.Append(indent); if (i.Attributes != null) { foreach (var attr in i.Attributes) { if (attributesNotToWrite.Contains(attr)) { continue; } sb.Append(attr.ToString()).Append(' '); } } sb.Append(i.ToCode(false)).Append(";").Append(eol); if (separatePackageRoots) { var iid = ImportComparer.ExtractPrimaryId(i); if (prevId != null && iid != null && (iid.InnerDeclaration ?? iid).ToString(true) != (prevId.InnerDeclaration ?? prevId).ToString(true)) { sb.Append(eol); } prevId = iid; } } editor.Insert(firstOffset, sb.ToString()); }