예제 #1
0
 public void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs args)
 {
     _diagnostics = args.GetPushDiagnostics(_globalOptions, InternalDiagnosticsOptions.NormalDiagnosticMode);
     DiagnosticsUpdated?.Invoke(this, args);
 }
 private void RaiseDiagnosticsRemoved(object id, Solution solution, ProjectId projectId, DocumentId documentId)
 {
     DiagnosticsUpdated?.Invoke(this, DiagnosticsUpdatedArgs.DiagnosticsRemoved(
                                    CreateArgumentKey(id), _workspace, solution, projectId, documentId));
 }
 public void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs args)
 => DiagnosticsUpdated?.Invoke(this, args);
예제 #4
0
 // ReSharper disable once UnusedParameter.Local
 private void OnDiagnosticsUpdated(object sender, Microsoft.CodeAnalysis.Diagnostics.DiagnosticsUpdatedArgs e)
 {
     DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedArgs(e));
 }
 private void RaiseDiagnosticsCreated(object id, Solution solution, ProjectId projectId, DocumentId documentId, ImmutableArray <DiagnosticData> items)
 {
     DiagnosticsUpdated?.Invoke(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                    CreateArgumentKey(id), _workspace, solution, projectId, documentId, items));
 }
 internal void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs state)
 => DiagnosticsUpdated?.Invoke(this, state);
예제 #7
0
 private void OnDocumentEvent(object sender, DocumentEventArgs e)
 {
     DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedEventArgs(e.Document));
 }
예제 #8
0
 public void RaiseUpdateEvent(DiagnosticsUpdatedArgs args)
 {
     DiagnosticsUpdated?.Invoke(this, args);
 }
예제 #9
0
 public void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs args)
 {
     this.diagnostics = args.Diagnostics;
     DiagnosticsUpdated?.Invoke(this, args);
 }
예제 #10
0
 /// <summary>
 /// Called from the roslyn host when diagnostics was updated.
 /// </summary>
 /// <param name="a">a.</param>
 public void EhDiagnosticsUpdated(DiagnosticsUpdatedArgs a)
 {
     DiagnosticsUpdated?.Invoke(a);
 }
 private void RaiseDiagnosticsUpdated(object id, ProjectId projectId, DocumentId documentId, ImmutableArray <DiagnosticData> items)
 {
     DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedArgs(
                                    new ArgumentKey(id), _workspace, _workspace.CurrentSolution, projectId, documentId, items));
 }
예제 #12
0
        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;
            }
        }
        public async Task <CodeAnalysisResults> RunCodeAnalysisAsync(IEditor editor,
                                                                     List <UnsavedFile> unsavedFiles, Func <bool> interruptRequested)
        {
            var result      = new CodeAnalysisResults();
            var diagnostics = new List <Diagnostic>();

            var file            = editor.SourceFile;
            var dataAssociation = GetAssociatedData(file);

            var currentUnsavedFile = unsavedFiles.FirstOrDefault(f => f.FileName == file.FilePath);
            var currentFileConts   = currentUnsavedFile?.Contents ?? File.ReadAllText(file.FilePath);
            var currentFileName    = currentUnsavedFile?.FileName ?? file.FilePath;
            TypeScriptSyntaxTree syntaxTree;
            // Only one analyzer at a time; the JS engine is single-threaded. TODO: Workaround with multiple JS engines
            await analysisThreadSemaphore.WaitAsync();

            try
            {
                syntaxTree = await _typeScriptContext.BuildAstAsync(currentFileName, currentFileConts);
            }
            catch (JavaScriptException)
            {
                diagnostics.Add(new Diagnostic(
                                    0, 0,
                                    editor.SourceFile.Project,
                                    editor.SourceFile.Location,
                                    0,
                                    "Code analysis language service call failed.",
                                    DiagnosticLevel.Error,
                                    DiagnosticCategory.Compiler));

                DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedEventArgs(this, editor.SourceFile, diagnostics.Count > 0 ? DiagnosticsUpdatedKind.DiagnosticsCreated : DiagnosticsUpdatedKind.DiagnosticsRemoved, diagnostics.ToImmutableArray()));

                return(new CodeAnalysisResults());
            }
            finally
            {
                analysisThreadSemaphore.Release();
            }

#if DEBUG
            var syntaxTreeJsonDebug = JsonConvert.SerializeObject(syntaxTree);
#endif

            var lineCommentMatches = LineCommentPattern.Matches(currentFileConts);
            foreach (Match commentMatch in lineCommentMatches)
            {
                result.SyntaxHighlightingData.Add(new OffsetSyntaxHighlightingData
                {
                    Start  = commentMatch.Index,
                    Length = commentMatch.Length,
                    Type   = HighlightType.Comment
                });
            }

            var blockCommentMatches = BlockCommentPattern.Matches(currentFileConts);
            foreach (Match commentMatch in blockCommentMatches)
            {
                result.SyntaxHighlightingData.Add(new OffsetSyntaxHighlightingData
                {
                    Start  = commentMatch.Index,
                    Length = commentMatch.Length,
                    Type   = HighlightType.Comment
                });
            }

            // Highlight keywords
            var keywordMatches = KeywordPattern.Matches(currentFileConts);
            foreach (Match keywordMatch in keywordMatches)
            {
                result.SyntaxHighlightingData.Add(new OffsetSyntaxHighlightingData
                {
                    Start  = keywordMatch.Index,
                    Length = keywordMatch.Length,
                    Type   = HighlightType.Keyword
                });
            }

            // Recursively highlight and analyze from parse tree
            foreach (var rootStatement in syntaxTree.Statements)
            {
                HighlightNode(rootStatement, result);
            }

            // Diagnostics
            foreach (var diagnostic in syntaxTree.ParseDiagnostics)
            {
                // Convert diagnostics
                diagnostics.Add(new Diagnostic(
                                    diagnostic.Start,
                                    diagnostic.Length,
                                    editor.SourceFile.Project,
                                    editor.SourceFile.Location,
                                    GetLineNumber(currentFileConts, diagnostic.Start),
                                    diagnostic.MessageText,
                                    diagnostic.Category == TSBridge.Ast.Diagnostics.Diagnostic.DiagnosticCategory.Error
                        ? DiagnosticLevel.Error
                        : DiagnosticLevel.Warning,
                                    DiagnosticCategory.Compiler
                                    ));
            }

            diagnostics.Add(new Diagnostic(
                                0,
                                0,
                                editor.SourceFile.Project,
                                editor.SourceFile.Location,
                                0,
                                "Code analysis for TypeScript is experimental and unstable. Use with caution.",
                                DiagnosticLevel.Warning,
                                DiagnosticCategory.Compiler));

            DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedEventArgs(this, editor.SourceFile, diagnostics.Count > 0 ? DiagnosticsUpdatedKind.DiagnosticsCreated : DiagnosticsUpdatedKind.DiagnosticsRemoved, diagnostics.ToImmutableArray()));

            return(result);
        }
예제 #14
0
 public void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs args)
 {
     _diagnostics = args.GetDiagnostics(_workspace, forPullDiagnostics: false);
     DiagnosticsUpdated?.Invoke(this, args);
 }