/// <summary> /// Creates a new TextPasteIndentEngine instance. /// </summary> /// <param name="decoratedEngine"> /// An instance of <see cref="IStateMachineIndentEngine"/> to which the /// logic for indentation will be delegated. /// </param> /// <param name="textEditorOptions"> /// Text editor options for indentation. /// </param> /// <param name="formattingOptions"> /// C# formatting options. /// </param> public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.engine = decoratedEngine; this.textEditorOptions = textEditorOptions; this.formattingOptions = formattingOptions; this.engine.EnableCustomIndentLevels = false; }
/// <summary> /// Creates a new TextPasteIndentEngine instance. /// </summary> /// <param name="decoratedEngine"> /// An instance of <see cref="IStateMachineIndentEngine"/> to which the /// logic for indentation will be delegated. /// </param> /// <param name = "options"></param> public TextPasteIndentEngine (IStateMachineIndentEngine decoratedEngine, OptionSet options) { this.engine = decoratedEngine; this.options = options; this.engine.EnableCustomIndentLevels = false; }
public bool FixLineStart (TextEditorData textEditorData, IStateMachineIndentEngine stateTracker, int lineNumber) { if (lineNumber > DocumentLocation.MinLine) { DocumentLine line = textEditorData.Document.GetLine (lineNumber); if (line == null) return false; DocumentLine prevLine = textEditorData.Document.GetLine (lineNumber - 1); if (prevLine == null) return false; string trimmedPreviousLine = textEditorData.Document.GetTextAt (prevLine).TrimStart (); //xml doc comments //check previous line was a doc comment //check there's a following line? if (trimmedPreviousLine.StartsWith ("///", StringComparison.Ordinal)) { if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("///", StringComparison.Ordinal)) return false; //check that the newline command actually inserted a newline textEditorData.EnsureCaretIsNotVirtual (); var nextLineSegment = textEditorData.Document.GetLine (lineNumber + 1); string nextLine = nextLineSegment != null ? textEditorData.Document.GetTextAt (nextLineSegment).TrimStart () : ""; if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith ("///", StringComparison.Ordinal)) { var insertionPoint = textEditorData.Caret.Offset; int inserted = textEditorData.Insert (insertionPoint, "/// "); textEditorData.Caret.Offset = insertionPoint + inserted; return true; } //multi-line comments } else if (stateTracker.IsInsideMultiLineComment) { if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("*", StringComparison.Ordinal)) return false; textEditorData.EnsureCaretIsNotVirtual (); string commentPrefix = string.Empty; if (trimmedPreviousLine.StartsWith ("* ", StringComparison.Ordinal)) { commentPrefix = "* "; } else if (trimmedPreviousLine.StartsWith ("/**", StringComparison.Ordinal) || trimmedPreviousLine.StartsWith ("/*", StringComparison.Ordinal)) { commentPrefix = " * "; } else if (trimmedPreviousLine.StartsWith ("*", StringComparison.Ordinal)) { commentPrefix = "*"; } int indentSize = line.GetIndentation (textEditorData.Document).Length; var insertedText = prevLine.GetIndentation (textEditorData.Document) + commentPrefix; textEditorData.Replace (line.Offset, indentSize, insertedText); textEditorData.Caret.Offset = line.Offset + insertedText.Length; return true; } else if (wasInStringLiteral) { var lexer = new CSharpCompletionEngineBase.MiniLexer (textEditorData.Document.GetTextAt (0, prevLine.EndOffset)); lexer.Parse (); if (!lexer.IsInString) return false; textEditorData.EnsureCaretIsNotVirtual (); textEditorData.Insert (prevLine.Offset + prevLine.Length, "\" +"); int indentSize = line.GetIndentation (textEditorData.Document).Length; var insertedText = prevLine.GetIndentation (textEditorData.Document) + (trimmedPreviousLine.StartsWith ("\"", StringComparison.Ordinal) ? "" : "\t") + "\""; textEditorData.Replace (line.Offset, indentSize, insertedText); return true; } } return false; }
/// <summary> /// Creates a new CacheIndentEngine instance from the given prototype. /// </summary> /// <param name="prototype"> /// A CacheIndentEngine instance. /// </param> public CacheIndentEngine(CacheIndentEngine prototype) { this.currentEngine = prototype.currentEngine.Clone(); }
/// <summary> /// Creates a new CacheIndentEngine instance. /// </summary> /// <param name="decoratedEngine"> /// An instance of <see cref="IStateMachineIndentEngine"/> to which the /// logic for indentation will be delegated. /// </param> /// <param name="cacheRate"> /// The number of chars between caching. /// </param> public CacheIndentEngine(IStateMachineIndentEngine decoratedEngine, int cacheRate = 2000) { this.currentEngine = decoratedEngine; }
/// <summary> /// Resets the engine to offset. Clears all cached engines after the given offset. /// </summary> public void ResetEngineToPosition(int offset) { // We are already there if (currentEngine.Offset <= offset) return; bool gotCachedEngine = false; while (cachedEngines.Count > 0) { var topEngine = cachedEngines.Peek(); if (topEngine.Offset <= offset) { currentEngine = topEngine.Clone(); gotCachedEngine = true; break; } else { cachedEngines.Pop(); } } if (!gotCachedEngine) currentEngine.Reset(); }
/// <summary> /// Creates a new TextPasteIndentEngine instance. /// </summary> /// <param name="decoratedEngine"> /// An instance of <see cref="IStateMachineIndentEngine"/> to which the /// logic for indentation will be delegated. /// </param> /// <param name = "options"></param> public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, OptionSet options) { this.engine = decoratedEngine; this.options = options; this.engine.EnableCustomIndentLevels = false; }
public bool FixLineStart(TextEditorData textEditorData, IStateMachineIndentEngine stateTracker, int lineNumber) { if (lineNumber > DocumentLocation.MinLine) { DocumentLine line = textEditorData.Document.GetLine(lineNumber); if (line == null) { return(false); } DocumentLine prevLine = textEditorData.Document.GetLine(lineNumber - 1); if (prevLine == null) { return(false); } string trimmedPreviousLine = textEditorData.Document.GetTextAt(prevLine).TrimStart(); //xml doc comments //check previous line was a doc comment //check there's a following line? if (trimmedPreviousLine.StartsWith("///", StringComparison.Ordinal)) { if (textEditorData.GetTextAt(line.Offset, line.Length).TrimStart().StartsWith("///", StringComparison.Ordinal)) { return(false); } //check that the newline command actually inserted a newline textEditorData.EnsureCaretIsNotVirtual(); var nextLineSegment = textEditorData.Document.GetLine(lineNumber + 1); string nextLine = nextLineSegment != null?textEditorData.Document.GetTextAt(nextLineSegment).TrimStart() : ""; if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith("///", StringComparison.Ordinal)) { var insertionPoint = textEditorData.Caret.Offset; int inserted = textEditorData.Insert(insertionPoint, "/// "); textEditorData.Caret.Offset = insertionPoint + inserted; return(true); } //multi-line comments } else if (stateTracker.IsInsideMultiLineComment) { if (textEditorData.GetTextAt(line.Offset, line.Length).TrimStart().StartsWith("*", StringComparison.Ordinal)) { return(false); } textEditorData.EnsureCaretIsNotVirtual(); string commentPrefix = string.Empty; if (trimmedPreviousLine.StartsWith("* ", StringComparison.Ordinal)) { commentPrefix = "* "; } else if (trimmedPreviousLine.StartsWith("/**", StringComparison.Ordinal) || trimmedPreviousLine.StartsWith("/*", StringComparison.Ordinal)) { commentPrefix = " * "; } else if (trimmedPreviousLine.StartsWith("*", StringComparison.Ordinal)) { commentPrefix = "*"; } int indentSize = line.GetIndentation(textEditorData.Document).Length; var insertedText = prevLine.GetIndentation(textEditorData.Document) + commentPrefix; textEditorData.Replace(line.Offset, indentSize, insertedText); textEditorData.Caret.Offset = line.Offset + insertedText.Length; return(true); } else if (wasInStringLiteral) { var lexer = new CSharpCompletionEngineBase.MiniLexer(textEditorData.Document.GetTextAt(0, prevLine.EndOffset)); lexer.Parse(); if (!lexer.IsInString) { return(false); } textEditorData.EnsureCaretIsNotVirtual(); textEditorData.Insert(prevLine.Offset + prevLine.Length, "\" +"); int indentSize = line.GetIndentation(textEditorData.Document).Length; var insertedText = prevLine.GetIndentation(textEditorData.Document) + (trimmedPreviousLine.StartsWith("\"", StringComparison.Ordinal) ? "" : "\t") + "\""; textEditorData.Replace(line.Offset, indentSize, insertedText); return(true); } } return(false); }