public static StringSplitter Create( Document document, int position, SyntaxNode root, SourceText sourceText, bool useTabs, int tabSize, IndentStyle indentStyle, CancellationToken cancellationToken) { var token = root.FindToken(position); if (token.IsKind(SyntaxKind.StringLiteralToken)) { return(new SimpleStringSplitter( document, position, root, sourceText, token, useTabs, tabSize, indentStyle, cancellationToken)); } var interpolatedStringExpression = TryGetInterpolatedStringExpression(token, position); if (interpolatedStringExpression != null) { return(new InterpolatedStringSplitter( document, position, root, sourceText, interpolatedStringExpression, useTabs, tabSize, indentStyle, cancellationToken)); } return(null); }
public virtual void CopyFrom(TextEditorOptions other) { zoom = other.zoom; highlightMatchingBracket = other.highlightMatchingBracket; tabsToSpaces = other.tabsToSpaces; indentationSize = other.indentationSize; tabSize = other.tabSize; showIconMargin = other.showIconMargin; showLineNumberMargin = other.showLineNumberMargin; showFoldMargin = other.showFoldMargin; showInvalidLines = other.showInvalidLines; showTabs = other.showTabs; showEolMarkers = other.showEolMarkers; highlightCaretLine = other.highlightCaretLine; showSpaces = other.showSpaces; rulerColumn = other.rulerColumn; showRuler = other.showRuler; indentStyle = other.indentStyle; fontName = other.fontName; enableSyntaxHighlighting = other.enableSyntaxHighlighting; colorStyle = other.colorStyle; overrideDocumentEolMarker = other.overrideDocumentEolMarker; defaultEolMarker = other.defaultEolMarker; enableAnimations = other.enableAnimations; useAntiAliasing = other.useAntiAliasing; DisposeFont(); OnChanged(EventArgs.Empty); }
private String GetIndentation(Int32 indent) { // safety to prevent errors if indent is < 0 if (indent < 0) { indent = 0; if (!hadIndentWarning) { Console.Out.WriteLine("WARNING: indent level < 0 in " + this.File); hadIndentWarning = true; } } String str = String.Empty; if (this.indent == IndentStyle.Tab) { str = " ".Substring(0, (indent * indentSize) % tabSize) + str; for (int x = 0; x < (indent * indentSize) / tabSize; x++) { str = "\t" + str; } return(str); } else { str = " ".Substring(0, indent * 4); return(str); } }
public virtual void CopyFrom(TextEditorOptions other) { Zoom = other.Zoom; highlightMatchingBracket = other.highlightMatchingBracket; tabsToSpaces = other.tabsToSpaces; indentationSize = other.indentationSize; tabSize = other.tabSize; showIconMargin = other.showIconMargin; showLineNumberMargin = other.showLineNumberMargin; showFoldMargin = other.showFoldMargin; highlightCaretLine = other.highlightCaretLine; rulerColumn = other.rulerColumn; showRuler = other.showRuler; indentStyle = other.indentStyle; fontName = other.fontName; enableSyntaxHighlighting = other.enableSyntaxHighlighting; colorStyle = other.colorStyle; overrideDocumentEolMarker = other.overrideDocumentEolMarker; defaultEolMarker = other.defaultEolMarker; enableAnimations = other.enableAnimations; drawIndentationMarkers = other.drawIndentationMarkers; showWhitespaces = other.showWhitespaces; includeWhitespaces = other.includeWhitespaces; generateFormattingUndoStep = other.generateFormattingUndoStep; DisposeFont(); OnChanged(EventArgs.Empty); }
private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync( string code, int indentationLine, int?expectedIndentation, IndentStyle indentStyle = IndentStyle.Smart) { // create tree service using (var workspace = TestWorkspace.CreateCSharp(code)) { workspace.Options = workspace.Options.WithChangedOption(SmartIndent, LanguageNames.CSharp, indentStyle); var hostdoc = workspace.Documents.First(); var buffer = hostdoc.GetTextBuffer(); var snapshot = buffer.CurrentSnapshot; var line = snapshot.GetLineFromLineNumber(indentationLine); var document = workspace.CurrentSolution.GetDocument(hostdoc.Id); var root = (await document.GetSyntaxRootAsync()) as CompilationUnitSyntax; Assert.False( CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter( Formatter.GetDefaultFormattingRules(workspace, root.Language), root, line.AsTextLine(), await document.GetOptionsAsync(), out _)); TestIndentation(workspace, indentationLine, expectedIndentation); } }
public BehaviorTextEditorPanelWidget() : base("EditorBindings.glade", "BehaviorTextEditorPanel") { // FIXME: enable when it is implemented autoinsertCurlyBraceCheckBox.Sensitive = false; // Set up Values autoinsertCurlyBraceCheckBox.Active = TextEditorProperties.AutoInsertCurlyBracket; autoInsertTemplatesCheckBox.Active = TextEditorProperties.AutoInsertTemplates; convertTabsToSpacesCheckBox.Active = TextEditorProperties.ConvertTabsToSpaces; //FIXME: Only one of these should be selected to hold the value indentAndTabSizeSpinButton.Value = TextEditorProperties.TabIndent; IndentStyle currentIndent = TextEditorProperties.IndentStyle; if (currentIndent == IndentStyle.None) { noneIndentStyle.Active = true; } else if (currentIndent == IndentStyle.Auto) { automaticIndentStyle.Active = true; } else if (currentIndent == IndentStyle.Smart) { smartIndentStyle.Active = true; } }
/// <summary> /// Inserts the PInvoke signature at the current cursor position. /// </summary> /// <param name="textArea">The text editor.</param> /// <param name="signature">A PInvoke signature string.</param> public void Generate(TextArea textArea, string signature) { IndentStyle oldIndentStyle = textArea.TextEditorProperties.IndentStyle; bool oldEnableEndConstructs = PropertyService.Get("VBBinding.TextEditor.EnableEndConstructs", true); try { textArea.BeginUpdate(); textArea.Document.UndoStack.StartUndoGroup(); textArea.TextEditorProperties.IndentStyle = IndentStyle.Smart; PropertyService.Set("VBBinding.TextEditor.EnableEndConstructs", false); string[] lines = signature.Replace("\r\n", "\n").Split('\n'); for (int i = 0; i < lines.Length; ++i) { textArea.InsertString(lines[i]); // Insert new line if not the last line. if (i < (lines.Length - 1)) { Return(textArea); } } } finally { textArea.Document.UndoStack.EndUndoGroup(); textArea.TextEditorProperties.IndentStyle = oldIndentStyle; PropertyService.Set("VBBinding.TextEditor.EnableEndConstructs", oldEnableEndConstructs); textArea.EndUpdate(); textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); textArea.Document.CommitUpdate(); } }
/// <summary> /// Inserts the snippet into the text editor at the current caret position. /// </summary> /// <param name="textArea">The text area.</param> public void Insert(TextArea textArea) { TextEditorControl textEditorControl = textArea.MotherTextEditorControl; IDocument document = textEditorControl.Document; document.UndoStack.StartUndoGroup(); //string selectedText = String.Empty; //if (textArea.SelectionManager.HasSomethingSelected) //{ // selectedText = textArea.SelectionManager.SelectedText; // textArea.Caret.Position = textArea.SelectionManager.Selections[0].StartPosition; // textArea.SelectionManager.RemoveSelectedText(); //} // ----- // SharpDevelop only: // The following line searches for the variable "$(Selection)" in the template text. // The variable is then replaced by the text that was previously selected in the editor. // string snippetText = StringParser.Parse(Text, new string[,] { { "Selection", selectedText } }); // We simply ignore the line above and just copy the text: string snippetText = Text; // ----- int finalCaretOffset = snippetText.IndexOf('|'); if (finalCaretOffset >= 0) { snippetText = snippetText.Remove(finalCaretOffset, 1); } else { finalCaretOffset = snippetText.Length; } int caretOffset = textArea.Caret.Offset; textEditorControl.BeginUpdate(); int beginLine = textArea.Caret.Line; document.Insert(caretOffset, snippetText); textArea.Caret.Position = document.OffsetToPosition(caretOffset + finalCaretOffset); int endLine = document.OffsetToPosition(caretOffset + snippetText.Length).Y; // Save old property settings IndentStyle save1 = textEditorControl.TextEditorProperties.IndentStyle; textEditorControl.TextEditorProperties.IndentStyle = IndentStyle.Smart; document.FormattingStrategy.IndentLines(textArea, beginLine, endLine); document.UndoStack.EndUndoGroup(); textEditorControl.EndUpdate(); document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); document.CommitUpdate(); // Restore old property settings textEditorControl.TextEditorProperties.IndentStyle = save1; }
public static ClassBuilder Create(ITypeSymbol typeSymbol, IndentStyle indentStyle = IndentStyle.Spaces) { var builder = Create(typeSymbol.ContainingNamespace, indentStyle); return(builder.AddClass(typeSymbol.Name) .WithAccessModifier(typeSymbol.DeclaredAccessibility) .OfType(typeSymbol.TypeKind)); }
public CodeWriter(IndentStyle indentStyle) { Indent = indentStyle switch { IndentStyle.Tabs => "\t", _ => " " }; }
public SimpleStringSplitter( Document document, int position, SyntaxNode root, SourceText sourceText, SyntaxToken token, bool useTabs, int tabSize, IndentStyle indentStyle, CancellationToken cancellationToken) : base(document, position, root, sourceText, useTabs, tabSize, indentStyle, cancellationToken) { _token = token; }
private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync( string code, int indentationLine, int?expectedIndentation, IndentStyle indentStyle = IndentStyle.Smart) { await AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(code, indentationLine, expectedIndentation, useTabs : false, indentStyle).ConfigureAwait(false); await AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(code.Replace(" ", "\t"), indentationLine, expectedIndentation, useTabs : true, indentStyle).ConfigureAwait(false); }
/// <summary> /// Write a line with specified style. /// </summary> /// <param name="style">The indentation style.</param> public void WriteLine(IndentStyle style) { sb.AppendLine(); Indent(style); for (int i = 0, n = depth * indent; i < n; i++) { sb.Append(" "); } }
public InterpolatedStringSplitter( Document document, int position, SyntaxNode root, SourceText sourceText, InterpolatedStringExpressionSyntax interpolatedStringExpression, bool useTabs, int tabSize, IndentStyle indentStyle, CancellationToken cancellationToken) : base(document, position, root, sourceText, useTabs, tabSize, indentStyle, cancellationToken) { _interpolatedStringExpression = interpolatedStringExpression; }
/// <remarks> /// This method inserts a code template at the current caret position /// </remarks> public void InsertTemplate(CodeTemplate template) { string selectedText = String.Empty; int undoActionCount = Document.UndoStack.UndoItemCount; Console.WriteLine("undoActionCount before " + undoActionCount); if (base.ActiveTextAreaControl.TextArea.SelectionManager.HasSomethingSelected) { selectedText = base.ActiveTextAreaControl.TextArea.SelectionManager.SelectedText; ActiveTextAreaControl.TextArea.Caret.Position = ActiveTextAreaControl.TextArea.SelectionManager.SelectionCollection[0].StartPosition; base.ActiveTextAreaControl.TextArea.SelectionManager.RemoveSelectedText(); } // save old properties, these properties cause strange effects, when not // be turned off (like insert curly braces or other formatting stuff) string templateText = StringParser.Parse(template.Text, new string[, ] { { "Selection", selectedText } }); int finalCaretOffset = templateText.IndexOf('|'); if (finalCaretOffset >= 0) { templateText = templateText.Remove(finalCaretOffset, 1); } else { finalCaretOffset = templateText.Length; } int caretOffset = ActiveTextAreaControl.TextArea.Caret.Offset; BeginUpdate(); int beginLine = ActiveTextAreaControl.TextArea.Caret.Line; Document.Insert(caretOffset, templateText); ActiveTextAreaControl.TextArea.Caret.Position = Document.OffsetToPosition(caretOffset + finalCaretOffset); int endLine = Document.OffsetToPosition(caretOffset + templateText.Length).Y; IndentStyle save1 = TextEditorProperties.IndentStyle; TextEditorProperties.IndentStyle = IndentStyle.Smart; Console.WriteLine("Indent between {0} and {1}", beginLine, endLine); Document.FormattingStrategy.IndentLines(ActiveTextAreaControl.TextArea, beginLine, endLine); Console.WriteLine("UndoItemCount after " + Document.UndoStack.UndoItemCount); Document.UndoStack.CombineLast(Document.UndoStack.UndoItemCount - undoActionCount); EndUpdate(); Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); Document.CommitUpdate(); // restore old property settings TextEditorProperties.IndentStyle = save1; }
/// <summary> /// verifyUndo is needed because of https://github.com/dotnet/roslyn/issues/28033 /// Most tests will continue to verifyUndo, but select tests will skip it due to /// this known test infrastructure issure. This bug does not represent a product /// failure. /// </summary> private void TestWorker( string inputMarkup, string expectedOutputMarkup, Action callback, bool verifyUndo = true, IndentStyle indentStyle = IndentStyle.Smart) { using var workspace = TestWorkspace.CreateCSharp(inputMarkup); workspace.Options = workspace.Options.WithChangedOption(SmartIndent, LanguageNames.CSharp, indentStyle); var document = workspace.Documents.Single(); var view = document.GetTextView(); var originalSnapshot = view.TextBuffer.CurrentSnapshot; var originalSelections = document.SelectedSpans; var snapshotSpans = new List <SnapshotSpan>(); foreach (var selection in originalSelections) { snapshotSpans.Add(selection.ToSnapshotSpan(originalSnapshot)); } view.SetMultiSelection(snapshotSpans); var undoHistoryRegistry = workspace.GetService <ITextUndoHistoryRegistry>(); var commandHandler = new SplitStringLiteralCommandHandler( undoHistoryRegistry, workspace.GetService <IEditorOperationsFactoryService>()); if (!commandHandler.ExecuteCommand(new ReturnKeyCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create())) { callback(); } if (expectedOutputMarkup != null) { MarkupTestFile.GetSpans(expectedOutputMarkup, out var expectedOutput, out ImmutableArray <TextSpan> expectedSpans); Assert.Equal(expectedOutput, view.TextBuffer.CurrentSnapshot.AsText().ToString()); Assert.Equal(expectedSpans.First().Start, view.Caret.Position.BufferPosition.Position); if (verifyUndo) { // Ensure that after undo we go back to where we were to begin with. var history = undoHistoryRegistry.GetHistory(document.GetTextBuffer()); history.Undo(count: originalSelections.Count); var currentSnapshot = document.GetTextBuffer().CurrentSnapshot; Assert.Equal(originalSnapshot.GetText(), currentSnapshot.GetText()); Assert.Equal(originalSelections.First().Start, view.Caret.Position.BufferPosition.Position); } } }
public int? GetDesiredIndentation(ITextSnapshotLine line, IndentStyle indentStyle) { if (line != null) { if (indentStyle == IndentStyle.Block) { return GetBlockIndent(line); } else if (indentStyle == IndentStyle.Smart) { return GetSmartIndent(line); } } return null; }
/// <summary> /// verifyUndo is needed because of https://github.com/dotnet/roslyn/issues/28033 /// Most tests will continue to verifyUndo, but select tests will skip it due to /// this known test infrastructure issure. This bug does not represent a product /// failure. /// </summary> private void TestHandled( string inputMarkup, string expectedOutputMarkup, bool verifyUndo = true, IndentStyle indentStyle = IndentStyle.Smart) { TestWorker( inputMarkup, expectedOutputMarkup, callback: () => { Assert.True(false, "Should not reach here."); }, verifyUndo, indentStyle); }
/// <summary> /// Indent by the specified style. /// </summary> /// <param name="style">The style.</param> protected virtual void Indent(IndentStyle style) { if (style == IndentStyle.Inner) { depth++; } else if (style == IndentStyle.Outer) { depth--; System.Diagnostics.Debug.Assert(depth >= 0); } }
private void SetIndentStyle(IndentStyle style) { if (textEditorControl.InvokeRequired) { Action <IndentStyle> action = SetIndentStyle; textEditorControl.Invoke(action, new object[] { style }); } else { textEditorControl.IndentStyle = style; } }
public void SetUpFixture() { textEditorControl = new TextEditorControl(); // Force creation of handle otherwise InvokeRequired always returns false. textEditorControl.CreateControl(); textEditorControl.IndentStyle = IndentStyle.Smart; defaultTextEditorControlIndentStyle = textEditorControl.IndentStyle; textEditor = new TextEditor(textEditorControl); defaultTextEditorIndentStyle = textEditor.IndentStyle; }
public StringSplitter( Document document, int position, SyntaxNode root, SourceText sourceText, bool useTabs, int tabSize, IndentStyle indentStyle, CancellationToken cancellationToken) { Document = document; CursorPosition = position; Root = root; SourceText = sourceText; UseTabs = useTabs; TabSize = tabSize; _indentStyle = indentStyle; CancellationToken = cancellationToken; }
public int?GetDesiredIndentation(IEditorLine line, IndentStyle indentStyle) { if (line != null) { switch (indentStyle) { case IndentStyle.Block: return(GetBlockIndent(line, _settings)); case IndentStyle.Smart: return(GetSmartIndent(line, _settings)); } } return(null); }
public int?GetDesiredIndentation(ITextSnapshotLine line, IndentStyle indentStyle) { if (line != null) { if (indentStyle == IndentStyle.Block) { return(GetBlockIndent(line)); } else if (indentStyle == IndentStyle.Smart) { return(GetSmartIndent(line)); } } return(null); }
private void GenerateIndentLine() { var sb = new StringBuilder(); for (int i = 0; i < Indent; i++) { sb.Append(IndentStyle); } _indentLine = sb.ToString(); sb.Clear(); sb.Append('\n'); sb.Append(_indentLine); _indentReplacer = sb.ToString(); _indentChars = IndentStyle.ToCharArray(); }
public static void Create(string indentStyleValue, bool indentStyleCallbackReturn, string indentSizeValue, bool indentSizeCallbackReturn, uint expectedIndentSize, IndentStyle expectedIndentStyle) { bool IndentStyleCallback(string key, [NotNullWhen(true)] out string?value) { value = indentStyleValue; return(indentStyleCallbackReturn); } bool IndentSizeCallback(string key, [NotNullWhen(true)] out string?value) { value = indentSizeValue; return(indentSizeCallbackReturn); } var tree = SyntaxFactory.ParseSyntaxTree("var id = 3;"); var options = Rock.Create <AnalyzerConfigOptions>(); options.Methods().TryGetValue("indent_size", Arg.Any <string?>()).Callback(IndentSizeCallback); options.Methods().TryGetValue("indent_style", Arg.Any <string?>()).Callback(IndentStyleCallback); var provider = Rock.Create <AnalyzerConfigOptionsProvider>(); provider.Methods().GetOptions(tree).Returns(options.Instance()); var configuration = new ConfigurationValues(provider.Instance(), tree); Assert.Multiple(() => { Assert.That(configuration.IndentSize, Is.EqualTo(expectedIndentSize), nameof(configuration.IndentSize)); Assert.That(configuration.IndentStyle, Is.EqualTo(expectedIndentStyle), nameof(configuration.IndentSize)); }); provider.Verify(); options.Verify(); }
private CodeBuilder(string clrNamespace, IndentStyle indentStyle = IndentStyle.Spaces) { Namespace = clrNamespace; IndentStyle = indentStyle; }
public Indenter(IndentStyle style, int count) { _char = style == IndentStyle.Space ? ' ' : '\t'; _count = count; _indentsCache = new Dictionary <int, XText>(); }
public static CodeBuilder Create(string clrNamespace, IndentStyle indentStyle = IndentStyle.Spaces) => new CodeBuilder(clrNamespace, indentStyle);
public bool TryGetIndentStyle(out IndentStyle indentStyle) { indentStyle = default(IndentStyle); return(false); }
public static CodeBuilder Create(INamespaceSymbol namespaceSymbol, IndentStyle indentStyle = IndentStyle.Spaces) => Create(namespaceSymbol.ToString(), indentStyle);
public virtual void CopyFrom (TextEditorOptions other) { zoom = other.zoom; highlightMatchingBracket = other.highlightMatchingBracket; tabsToSpaces = other.tabsToSpaces; indentationSize = other.indentationSize; tabSize = other.tabSize; showIconMargin = other.showIconMargin; showLineNumberMargin = other.showLineNumberMargin; showFoldMargin = other.showFoldMargin; highlightCaretLine = other.highlightCaretLine; rulerColumn = other.rulerColumn; showRuler = other.showRuler; indentStyle = other.indentStyle; fontName = other.fontName; enableSyntaxHighlighting = other.enableSyntaxHighlighting; colorStyle = other.colorStyle; overrideDocumentEolMarker = other.overrideDocumentEolMarker; defaultEolMarker = other.defaultEolMarker; enableAnimations = other.enableAnimations; useAntiAliasing = other.useAntiAliasing; drawIndentationMarkers = other.drawIndentationMarkers; showWhitespaces = other.showWhitespaces; includeWhitespaces = other.includeWhitespaces; DisposeFont (); OnChanged (EventArgs.Empty); }
void SetIndentStyle(IndentStyle style) { if (textEditorControl.InvokeRequired) { Action<IndentStyle> action = SetIndentStyle; textEditorControl.Invoke(action, new object[] { style }); } else { textEditorControl.IndentStyle = style; } }