Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore diagnostics for generated code #2509

Merged
merged 31 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e5e8197
save
Anakael Feb 26, 2023
bd4b529
refactor filter
Anakael Feb 26, 2023
241eebe
finish
Anakael Feb 26, 2023
a715274
fix format
Anakael Feb 26, 2023
affb51b
remove unused usings
Anakael Feb 26, 2023
42e01f1
fix build
Anakael Feb 27, 2023
bc63a7c
finish
Anakael Mar 3, 2023
090f525
revert logging option
Anakael Jun 1, 2023
87ef8bf
format
Anakael Jun 1, 2023
dfcded5
extract exetnsion
Anakael Jun 6, 2023
5c256b3
reverse matcher
Anakael Jun 6, 2023
fb90c5d
Update src/OmniSharp.Shared/FileSystem/FileSystemHelper.cs
Anakael Jun 19, 2023
7f4ec22
Update src/OmniSharp.Roslyn.CSharp/Workers/Diagnostics/CSharpDiagnost…
Anakael Jun 19, 2023
2b89bba
try fix some tests
Anakael Jun 19, 2023
4689ea0
fix comments
Anakael Jun 20, 2023
3552ec1
update env
Anakael Jun 20, 2023
da0631f
fix compiling
Anakael Jun 20, 2023
c5b9a41
Update TestAssets RootFolder with ending slash.
JoeRobich Jun 20, 2023
32d7ffb
Merge branch 'master' into pr/anakael/fix-ignore
JoeRobich Jun 20, 2023
ea94b76
Merge branch 'master' into pr/anakael/fix-ignore
JoeRobich Jul 14, 2023
f82e6a2
Merge branch 'master' of github.com:OmniSharp/omnisharp-roslyn into p…
Anakael Jul 19, 2023
abf93de
try increase timeout
Anakael Jul 19, 2023
2e33df6
Merge branch 'OmniSharp:master' into pr/anakael/fix-ignore
Anakael Jul 19, 2023
e4deb92
Merge branch 'pr/anakael/fix-ignore' of github.com:Anakael/omnisharp-…
Anakael Jul 19, 2023
ecd285b
Merge branch 'master' of github.com:OmniSharp/omnisharp-roslyn into p…
Anakael Aug 18, 2023
fe6a29d
change test
Anakael Aug 19, 2023
83fe01c
decrease timeout
Anakael Aug 19, 2023
08af024
remove test
Anakael Aug 19, 2023
5da876f
fix test
Anakael Aug 19, 2023
d7c9fc1
revert file
Anakael Aug 19, 2023
e2ddf93
add tabs
Anakael Aug 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/OmniSharp.Host/Services/OmniSharpEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public OmniSharpEnvironment(
throw new ArgumentException("OmniSharp only supports being launched with a directory path or a path to a solution (.sln, .slnf) file.", nameof(path));
}

if (TargetDirectory[TargetDirectory.Length - 1] != Path.DirectorySeparatorChar)
{
TargetDirectory += Path.DirectorySeparatorChar;
}

HostProcessId = hostPid;
LogLevel = logLevel;
AdditionalArguments = additionalArguments;
Expand Down
50 changes: 50 additions & 0 deletions src/OmniSharp.Roslyn.CSharp/Helpers/SyntaxTreeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace OmniSharp.Roslyn.CSharp.Helpers
{
public static class SyntaxTreeExtensions
{
private static readonly string[] _autoGeneratedStrings = new[] { "<autogenerated", "<auto-generated" };

public static bool IsAutoGenerated(this SyntaxTree syntaxTree, CancellationToken cancellationToken = default)
{
SyntaxNode root = syntaxTree.GetRoot(cancellationToken);
if (!root.HasLeadingTrivia)
{
return false;
}

SyntaxTriviaList leadingTrivia = root.GetLeadingTrivia();

foreach (SyntaxTrivia trivia in leadingTrivia)
{
if (!IsCommentTrivia(trivia))
{
continue;
}

foreach (string autoGenerated in _autoGeneratedStrings)
{
if (!trivia.ToString().Contains(autoGenerated))
{
continue;
}

return true;
}
}

return false;
}

private static bool IsCommentTrivia(SyntaxTrivia trivia) =>
trivia.IsKind(SyntaxKind.MultiLineCommentTrivia) ||
trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) ||
trivia.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia) ||
trivia.IsKind(SyntaxKind.MultiLineDocumentationCommentTrivia) ||
trivia.IsKind(SyntaxKind.ShebangDirectiveTrivia);

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.FileSystemGlobbing;
using OmniSharp.FileSystem;
using OmniSharp.Roslyn.CSharp.Services.Diagnostics;

namespace OmniSharp.Roslyn.CSharp.Workers.Diagnostics;

public abstract class CSharpDiagnosticWorkerBase : ICsDiagnosticWorker
{
private readonly OmniSharpWorkspace _workspace;
private readonly FileSystemHelper _fileSystemHelper;
private readonly Matcher _fileSystemMatcher;

public CSharpDiagnosticWorkerBase(
OmniSharpWorkspace workspace,
FileSystemHelper fileSystemHelper)
{
_workspace = workspace;
_fileSystemHelper = fileSystemHelper;
_fileSystemMatcher = _fileSystemHelper.BuildExcludeMatcher();
}

public abstract bool AnalyzersEnabled { get; }

public abstract Task<IEnumerable<Diagnostic>> AnalyzeDocumentAsync(Document document, CancellationToken cancellationToken);

public abstract Task<IEnumerable<Diagnostic>> AnalyzeProjectsAsync(Project project, CancellationToken cancellationToken);

public abstract Task<ImmutableArray<DocumentDiagnostics>> GetAllDiagnosticsAsync();

public abstract Task<ImmutableArray<DocumentDiagnostics>> GetDiagnostics(ImmutableArray<string> documentPaths);

public abstract ImmutableArray<DocumentId> QueueDocumentsForDiagnostics(IEnumerable<Document> documents, AnalyzerWorkType workType);

public ImmutableArray<DocumentId> QueueDocumentsForDiagnostics()
{
IEnumerable<Document> documents = _workspace.CurrentSolution.Projects.SelectMany(x => x.Documents);
documents = FilterDocuments(documents);
return QueueDocumentsForDiagnostics(documents, AnalyzerWorkType.Background);
}

public ImmutableArray<DocumentId> QueueDocumentsForDiagnostics(ImmutableArray<ProjectId> projectId)
{
IEnumerable<Document> documents = projectId.SelectMany(projectId => _workspace.CurrentSolution.GetProject(projectId).Documents);
documents = FilterDocuments(documents);
return QueueDocumentsForDiagnostics(documents, AnalyzerWorkType.Background);
}

protected ImmutableArray<DocumentId> QueueDocumentsForDiagnostics(Project project)
{
IEnumerable<Document> documents = project.Documents;
documents = FilterDocuments(documents);
return QueueDocumentsForDiagnostics(documents, AnalyzerWorkType.Background);
}

protected ImmutableArray<DocumentId> QueueDocumentsForDiagnostics(IEnumerable<DocumentId> documentsIds, AnalyzerWorkType workType)
{
IEnumerable<Document> documents = documentsIds.Select(x => _workspace.CurrentSolution.GetDocument(x));
documents = FilterDocuments(documents);
return QueueDocumentsForDiagnostics(documents, workType);
}

private ImmutableArray<Document> FilterDocuments(IEnumerable<Document> documents) =>
documents
.Where(x => !FileSystemHelper.AutoGenerated(x.FilePath))
.Where(x => !_fileSystemMatcher
.Match(FileSystemHelper
.GetRelativePath(x.FilePath, _fileSystemHelper.TargetDirectory)
?? x.FilePath)
.HasMatches)
.ToImmutableArray();
}
Loading