public void RegisterSourceFile(AvaloniaEdit.TextEditor editor, ISourceFile file, TextDocument doc) { CSharpDataAssociation association = null; if (dataAssociations.TryGetValue(file, out association)) { throw new Exception("Source file already registered with language service."); } IndentationStrategy = new CSharpIndentationStrategy(editor.Options); association = new CSharpDataAssociation(); association.Solution = file.Project.Solution as OmniSharpSolution; // CanHandle has checked this. dataAssociations.Add(file, association); association.TextInputHandler = (sender, e) => { if (editor.Document == doc) { editor.BeginChange(); OpenBracket(editor, editor.Document, e.Text); CloseBracket(editor, editor.Document, e.Text); switch (e.Text) { case "}": case ";": editor.CaretOffset = Format(editor.Document, 0, (uint)editor.Document.TextLength, editor.CaretOffset); break; case "{": var lineCount = editor.Document.LineCount; var offset = Format(editor.Document, 0, (uint)editor.Document.TextLength, editor.CaretOffset); // suggests clang format didnt do anything, so we can assume not moving to new line. if (lineCount != editor.Document.LineCount) { if (offset <= editor.Document.TextLength) { var newLine = editor.Document.GetLineByOffset(offset); editor.CaretOffset = newLine.PreviousLine.EndOffset; } } else { editor.CaretOffset = offset; } break; } editor.EndChange(); } }; editor.TextArea.TextEntered += association.TextInputHandler; }
private CSharpDataAssociation GetAssociatedData(ISourceFile sourceFile) { CSharpDataAssociation result = null; if (!dataAssociations.TryGetValue(sourceFile, out result)) { throw new Exception("Tried to parse file that has not been registered with the language service."); } return(result); }
public void RegisterEditor(ITextEditor editor) { _editor = editor; if (dataAssociations.TryGetValue(editor, out CSharpDataAssociation association)) { throw new Exception("Source file already registered with language service."); } association = new CSharpDataAssociation { Solution = editor.SourceFile.Project.Solution }; dataAssociations.Add(editor, association); if (!(editor.SourceFile is MetaDataFile)) { var avaloniaEditTextContainer = new AvalonEditTextContainer(editor.Document) { Editor = editor }; RoslynWorkspace.GetWorkspace(association.Solution).OpenDocument(editor.SourceFile, avaloniaEditTextContainer, (diagnostics) => { var dataAssociation = GetAssociatedData(editor); var results = new List <Diagnostic>(); var fadedCode = new SyntaxHighlightDataList(); foreach (var diagnostic in diagnostics.Diagnostics) { if (diagnostic.CustomTags.Contains("Unnecessary")) { fadedCode.Add(new OffsetSyntaxHighlightingData { Start = diagnostic.TextSpan.Start, Length = diagnostic.TextSpan.Length, Type = HighlightType.Unnecessary }); } else { results.Add(FromRoslynDiagnostic(diagnostic, editor.SourceFile.Location, editor.SourceFile.Project)); } } var errorList = IoC.Get <IErrorList>(); errorList.Remove((diagnostics.Id, editor.SourceFile)); errorList.Create((diagnostics.Id, editor.SourceFile), editor.SourceFile.FilePath, DiagnosticSourceKind.Analysis, results.ToImmutableArray(), fadedCode); });
public void RegisterSourceFile(IIntellisenseControl intellisenseControl, ICompletionAssistant completionAssistant, TextEditor editor, ISourceFile file, TextDocument textDocument) { CSharpDataAssociation association = null; if (dataAssociations.TryGetValue(file, out association)) { throw new Exception("Source file already registered with language service."); } association = new CSharpDataAssociation(textDocument); association.Solution = file.Project.Solution as OmniSharpSolution; // CanHandle has checked this. dataAssociations.Add(file, association); association.KeyUpHandler = (sender, e) => { if (editor.TextDocument == textDocument) { switch (e.Key) { case Key.Return: { if (editor.CaretIndex >= 0 && editor.CaretIndex < editor.TextDocument.TextLength) { if (editor.TextDocument.GetCharAt(editor.CaretIndex) == '}') { editor.TextDocument.Insert(editor.CaretIndex, Environment.NewLine); editor.CaretIndex--; var currentLine = editor.TextDocument.GetLineByOffset(editor.CaretIndex); editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine, editor.CaretIndex); editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine.NextLine.NextLine, editor.CaretIndex); editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine.NextLine, editor.CaretIndex); } var newCaret = IndentationStrategy.IndentLine(editor.TextDocument, editor.TextDocument.GetLineByOffset(editor.CaretIndex), editor.CaretIndex); editor.CaretIndex = newCaret; } } break; } } }; editor.AddHandler(InputElement.KeyUpEvent, association.KeyUpHandler, RoutingStrategies.Tunnel); }
private Microsoft.CodeAnalysis.Document GetDocument(CSharpDataAssociation dataAssociation, ISourceFile file, RoslynWorkspace workspace = null) { if (file is MetaDataFile metaDataFile) { return(metaDataFile.Document); } else { if (workspace == null) { workspace = RoslynWorkspace.GetWorkspace(dataAssociation.Solution); } return(workspace.GetDocument(file)); } }
public void RegisterSourceFile(IEditor editor) { if (dataAssociations.TryGetValue(editor, out CSharpDataAssociation association)) { throw new Exception("Source file already registered with language service."); } IndentationStrategy = new CSharpIndentationStrategy(new AvaloniaEdit.TextEditorOptions { ConvertTabsToSpaces = true }); association = new CSharpDataAssociation { Solution = editor.SourceFile.Project.Solution }; dataAssociations.Add(editor, association); if (!(editor.SourceFile is MetaDataFile)) { var avaloniaEditTextContainer = new AvalonEditTextContainer(editor.Document) { Editor = editor }; RoslynWorkspace.GetWorkspace(association.Solution).OpenDocument(editor.SourceFile, avaloniaEditTextContainer, (diagnostics) => { var dataAssociation = GetAssociatedData(editor); //var results = new TextSegmentCollection<Diagnostic>(); //foreach (var diagnostic in diagnostics.Diagnostics) //{ // results.Add(FromRoslynDiagnostic(diagnostic, editor.SourceFile.Location, editor.SourceFile.Project)); //} //(Diagnostics as Subject<TextSegmentCollection<Diagnostic>>).OnNext(results); }); association.TextInputHandler = (sender, e) => { switch (e.Text) { case "}": case ";": editor.IndentLine(editor.Line); break; case "{": if (IndentationStrategy != null) { editor.IndentLine(editor.Line); } break; } OpenBracket(editor, editor.Document, e.Text); CloseBracket(editor, editor.Document, e.Text); }; association.BeforeTextInputHandler = (sender, e) => { switch (e.Text) { case "\n": case "\r\n": var nextChar = ' '; if (editor.CaretOffset != editor.Document.TextLength) { nextChar = editor.Document.GetCharAt(editor.CaretOffset); } if (nextChar == '}') { var newline = "\r\n"; // TextUtilities.GetNewLineFromDocument(editor.Document, editor.TextArea.Caret.Line); editor.Document.Insert(editor.CaretOffset, newline); editor.Document.TrimTrailingWhiteSpace(editor.Line - 1); editor.IndentLine(editor.Line); editor.CaretOffset -= newline.Length; } break; } }; editor.TextEntered += association.TextInputHandler; editor.TextEntering += association.BeforeTextInputHandler; } }
public void RegisterSourceFile(IEditor editor) { if (dataAssociations.TryGetValue(editor, out CSharpDataAssociation association)) { throw new Exception("Source file already registered with language service."); } IndentationStrategy = new CSharpIndentationStrategy(new AvaloniaEdit.TextEditorOptions { ConvertTabsToSpaces = true }); association = new CSharpDataAssociation { Solution = editor.SourceFile.Project.Solution }; dataAssociations.Add(editor, association); if (!(editor.SourceFile is MetaDataFile)) { var avaloniaEditTextContainer = new AvalonEditTextContainer(editor.Document) { Editor = editor }; RoslynWorkspace.GetWorkspace(association.Solution).OpenDocument(editor.SourceFile, avaloniaEditTextContainer, (diagnostics) => { var dataAssociation = GetAssociatedData(editor); var results = new List <Diagnostic>(); var fadedCode = new SyntaxHighlightDataList(); foreach (var diagnostic in diagnostics.Diagnostics) { if (diagnostic.CustomTags.Contains("Unnecessary")) { fadedCode.Add(new OffsetSyntaxHighlightingData { Start = diagnostic.TextSpan.Start, Length = diagnostic.TextSpan.Length, Type = HighlightType.Unnecessary }); } else { results.Add(FromRoslynDiagnostic(diagnostic, editor.SourceFile.Location, editor.SourceFile.Project)); } } DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedEventArgs(diagnostics.Id, editor.SourceFile, (DiagnosticsUpdatedKind)diagnostics.Kind, results.ToImmutableArray(), fadedCode)); }); association.TextInputHandler = (sender, e) => { switch (e.Text) { case "}": case ";": editor.IndentLine(editor.Line); break; case "{": if (IndentationStrategy != null) { editor.IndentLine(editor.Line); } break; } OpenBracket(editor, editor.Document, e.Text); CloseBracket(editor, editor.Document, e.Text); }; association.BeforeTextInputHandler = (sender, e) => { switch (e.Text) { case "\n": case "\r\n": var nextChar = ' '; if (editor.CaretOffset != editor.Document.TextLength) { nextChar = editor.Document.GetCharAt(editor.CaretOffset); } if (nextChar == '}') { var newline = "\r\n"; // TextUtilities.GetNewLineFromDocument(editor.Document, editor.TextArea.Caret.Line); editor.Document.Insert(editor.CaretOffset, newline); editor.Document.TrimTrailingWhiteSpace(editor.Line - 1); editor.IndentLine(editor.Line); editor.CaretOffset -= newline.Length; } break; } }; editor.TextEntered += association.TextInputHandler; editor.TextEntering += association.BeforeTextInputHandler; } }