public void SetTransformations(SyntaxHighlightDataList highlightData) { Dispatcher.UIThread.InvokeAsync(() => { var transformations = new TextSegmentCollection<TextTransformation>(document); foreach (var transform in highlightData) { if (transform.Type != HighlightType.None) { transformations.Add(new TextTransformation { Foreground = GetBrush(transform.Type), StartOffset = transform.Start, EndOffset = transform.Start + transform.Length }); } } TextTransformations = transformations; if (DataChanged != null) { DataChanged(this, new EventArgs()); } }); }
public void SetTransformations(SyntaxHighlightDataList highlightData) { Dispatcher.UIThread.InvokeAsync(() => { var transformations = new TextSegmentCollection <TextTransformation>(document); foreach (var transform in highlightData) { if (transform.Type != HighlightType.None) { transformations.Add(new TextTransformation { Foreground = GetBrush(transform.Type), StartOffset = transform.Start, EndOffset = transform.Start + transform.Length }); } } TextTransformations = transformations; if (DataChanged != null) { DataChanged(this, new EventArgs()); } }); }
private void GenerateHighlightData(ClangCursor cursor, SyntaxHighlightDataList highlightList, List <IndexEntry> result) { cursor.VisitChildren((current, parent, ptr) => { if (current.Location.IsFromMainFile) { var highlight = CreateOffsetData(current, parent); if (highlight != null) { highlightList.Add(highlight); } switch (current.Kind) { case NClang.CursorKind.CompoundStatement: case NClang.CursorKind.ClassDeclaration: case NClang.CursorKind.Namespace: result.Add(new IndexEntry(current.Spelling, current.CursorExtent.Start.FileLocation.Offset, current.CursorExtent.End.FileLocation.Offset, (CursorKind)current.Kind)); break; } return(ChildVisitResult.Recurse); } if (current.Location.IsInSystemHeader) { return(ChildVisitResult.Continue); } return(ChildVisitResult.Recurse); }, IntPtr.Zero); }
private void ScanTokens(NClang.ClangTranslationUnit tu, SyntaxHighlightDataList result) { var tokens = tu.Tokenize(tu.GetCursor().CursorExtent); //var annotatedTokens = tokens.Annotate(); //TODO see if this can provide us with additional data. foreach (var token in tokens.Tokens) { var highlightData = new OffsetSyntaxHighlightingData(); highlightData.Start = token.Extent.Start.FileLocation.Offset; highlightData.Length = token.Extent.End.FileLocation.Offset - highlightData.Start; switch (token.Kind) { case TokenKind.Comment: highlightData.Type = HighlightType.Comment; result.Add(highlightData); break; case TokenKind.Keyword: highlightData.Type = HighlightType.Keyword; result.Add(highlightData); break; } } }
private void ScanTokens(NClang.ClangTranslationUnit tu, SyntaxHighlightDataList result) { var tokens = tu.Tokenize(tu.GetCursor().CursorExtent); foreach (var token in tokens.Tokens) { var highlightData = new OffsetSyntaxHighlightingData { Start = token.Extent.Start.FileLocation.Offset }; highlightData.Length = token.Extent.End.FileLocation.Offset - highlightData.Start; switch (token.Kind) { case TokenKind.Comment: highlightData.Type = HighlightType.Comment; result.Add(highlightData); break; case TokenKind.Keyword: highlightData.Type = HighlightType.Keyword; result.Add(highlightData); break; } } }
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); });
private void GenerateHighlightData(ClangCursor cursor, SyntaxHighlightDataList highlightList) { cursor.VisitChildren((current, parent, ptr) => { if (current.Location.IsFromMainFile) { var highlight = CreateOffsetData(current, parent); if (highlight != null) { highlightList.Add(highlight); } return(ChildVisitResult.Recurse); } if (current.Location.IsInSystemHeader) { return(ChildVisitResult.Continue); } return(ChildVisitResult.Recurse); }, IntPtr.Zero); }
/// <inheritdoc/> public void Create(object tag, string filePath, DiagnosticSourceKind source, ImmutableArray <Diagnostic> diagnostics, SyntaxHighlightDataList diagnosticHighlights = null) { Dispatcher.UIThread.Post(() => { foreach (var diagnostic in diagnostics) { if (diagnostic.Level != DiagnosticLevel.Hidden) { Errors.InsertSorted(new ErrorViewModel(diagnostic, tag)); } } DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedEventArgs(tag, filePath, DiagnosticsUpdatedKind.DiagnosticsCreated, source, diagnostics, diagnosticHighlights)); }); }
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; } }