internal EditorConfigDocumentOptionsProvider(Workspace workspace) { _codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager(); workspace.DocumentOpened += Workspace_DocumentOpened; workspace.DocumentClosed += Workspace_DocumentClosed; }
public EditorConfigDocumentOptionsProviderFactory( ICodingConventionsManager codingConventionsManager, IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider) { _codingConventionsManager = codingConventionsManager; _asynchronousOperationListenerProvider = asynchronousOperationListenerProvider; }
internal EditorConfigDocumentOptionsProvider(Workspace workspace) { _codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager(); workspace.DocumentOpened += Workspace_DocumentOpened; workspace.DocumentClosed += Workspace_DocumentClosed; }
internal EditorConfigDocumentOptionsProvider(Workspace workspace) { _codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager(); _errorLogger = workspace.Services.GetService <IErrorLoggerService>(); workspace.DocumentOpened += Workspace_DocumentOpened; workspace.DocumentClosed += Workspace_DocumentClosed; }
/// <summary> /// Try to load guideline positions from .editorconfig /// </summary> /// <param name="codingConventionsManager">The coding conventions (.editorconfig) manager.</param> /// <param name="filePath">Path to the document being edited.</param> /// <returns>A task which completes when the convention has been loaded and applied.</returns> private async Task LoadGuidelinesFromEditorConfigAsync(ICodingConventionsManager codingConventionsManager, string filePath) { var cancellationToken = _codingConventionsCancellationTokenSource.Token; var codingConventionContext = await codingConventionsManager.GetConventionContextAsync(filePath, cancellationToken).ConfigureAwait(false); codingConventionContext.CodingConventionsChangedAsync += OnCodingConventionsChangedAsync; cancellationToken.Register(() => codingConventionContext.CodingConventionsChangedAsync -= OnCodingConventionsChangedAsync); await UpdateGuidelinesFromCodingConventionAsync(codingConventionContext, cancellationToken).ConfigureAwait(false); }
internal LegacyEditorConfigDocumentOptionsProvider(Workspace workspace, ICodingConventionsManager codingConventionsManager, IAsynchronousOperationListenerProvider listenerProvider) { _workspace = workspace; _listener = listenerProvider.GetListener(FeatureAttribute.Workspace); _codingConventionsManager = codingConventionsManager; workspace.DocumentOpened += Workspace_DocumentOpened; workspace.DocumentClosed += Workspace_DocumentClosed; // workaround until this is fixed. // https://github.com/dotnet/roslyn/issues/26377 // otherwise, we will leak files in _openDocumentContexts workspace.WorkspaceChanged += Workspace_WorkspaceChanged; }
private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, ICodingConventionsManager codingConventionsManager) { var tree = context.Tree; var cancellationToken = context.CancellationToken; OptionSet options = CompilerAnalyzerConfigOptions.Empty; if (File.Exists(tree.FilePath)) { var codingConventionContext = codingConventionsManager.GetConventionContextAsync(tree.FilePath, cancellationToken).GetAwaiter().GetResult(); options = ApplyFormattingOptions(options, codingConventionContext); } FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormattingService, Descriptor, options); }
private static async Task <(Solution solution, int filesFormatted)> FormatFilesInProjectAsync(ILogger logger, Project project, ICodingConventionsManager codingConventionsManager, EditorConfigOptionsApplier optionsApplier, string[] filesToFormat, CancellationToken cancellationToken) { var isCommentTrivia = project.Language == LanguageNames.CSharp ? IsCSharpCommentTrivia : IsVisualBasicCommentTrivia; var formattedDocuments = new List <(DocumentId documentId, Task <SourceText> formatTask)>(); foreach (var documentId in project.DocumentIds) { var document = project.Solution.GetDocument(documentId); if (!document.SupportsSyntaxTree) { continue; } if (filesToFormat != null) { var fileInArgumentList = filesToFormat.Any(relativePath => document.FilePath.EndsWith(relativePath, StringComparison.OrdinalIgnoreCase)); if (!fileInArgumentList) { continue; } } var formatTask = Task.Run(async() => { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (GeneratedCodeUtilities.IsGeneratedCode(syntaxTree, isCommentTrivia, cancellationToken)) { return(null); } logger.LogTrace(Resources.Formatting_code_file_0, Path.GetFileName(document.FilePath)); OptionSet documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var codingConventionsContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath, cancellationToken).ConfigureAwait(false); if (codingConventionsContext?.CurrentConventions != null) { documentOptions = optionsApplier.ApplyConventions(documentOptions, codingConventionsContext.CurrentConventions, project.Language); } var formattedDocument = await Formatter.FormatAsync(document, documentOptions, cancellationToken).ConfigureAwait(false); var formattedSourceText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); if (formattedSourceText.ContentEquals(await document.GetTextAsync(cancellationToken).ConfigureAwait(false))) { // Avoid touching files that didn't actually change return(null); } return(formattedSourceText); }, cancellationToken); formattedDocuments.Add((documentId, formatTask)); } var formattedSolution = project.Solution; var filesFormatted = 0; foreach (var(documentId, formatTask) in formattedDocuments) { var text = await formatTask.ConfigureAwait(false); if (text is null) { continue; } filesFormatted++; formattedSolution = formattedSolution.WithDocumentText(documentId, text); } return(formattedSolution, filesFormatted); }
private static async Task <(int status, int fileCount, int filesFormatted)> FormatFilesInWorkspaceAsync(ILogger logger, Workspace workspace, string projectPath, ICodingConventionsManager codingConventionsManager, bool saveFormattedFiles, string[] filesToFormat, CancellationToken cancellationToken) { var projectIds = workspace.CurrentSolution.ProjectIds.ToImmutableArray(); var optionsApplier = new EditorConfigOptionsApplier(); var totalFileCount = 0; var totalFilesFormatted = 0; foreach (var projectId in projectIds) { var project = workspace.CurrentSolution.GetProject(projectId); if (!string.IsNullOrEmpty(projectPath) && !project.FilePath.Equals(projectPath, StringComparison.OrdinalIgnoreCase)) { logger.LogDebug(Resources.Skipping_referenced_project_0, project.Name); continue; } if (project.Language != LanguageNames.CSharp && project.Language != LanguageNames.VisualBasic) { logger.LogWarning(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, project.FilePath); continue; } logger.LogInformation(Resources.Formatting_code_files_in_project_0, project.Name); var(formattedSolution, filesFormatted) = await FormatFilesInProjectAsync(logger, project, codingConventionsManager, optionsApplier, filesToFormat, cancellationToken).ConfigureAwait(false); totalFileCount += project.DocumentIds.Count; totalFilesFormatted += filesFormatted; if (saveFormattedFiles && !workspace.TryApplyChanges(formattedSolution)) { logger.LogError(Resources.Failed_to_save_formatting_changes); return(1, totalFileCount, totalFilesFormatted); } } return(0, totalFileCount, totalFilesFormatted); }
/// <summary> /// Creates editor column guidelines /// </summary> /// <param name="view">The <see cref="IWpfTextView"/> upon which the adornment will be drawn</param> /// <param name="settings">The guideline settings.</param> /// <param name="guidelineBrush">The guideline brush.</param> /// <param name="codingConventionsManager">The coding conventions manager for handling .editorconfig settings.</param> /// <param name="telemetry">Telemetry interface.</param> public ColumnGuide(IWpfTextView view, ITextEditorGuidesSettings settings, GuidelineBrush guidelineBrush, ICodingConventionsManager codingConventionsManager) { _view = view; _guidelineBrush = guidelineBrush; _guidelineBrush.BrushChanged += GuidelineBrushChanged; _strokeParameters = StrokeParameters.FromBrush(_guidelineBrush.Brush); if (codingConventionsManager != null && view.TryGetTextDocument(out var textDocument)) { _codingConventionsCancellationTokenSource = new CancellationTokenSource(); var fireAndForgetTask = LoadGuidelinesFromEditorConfigAsync(codingConventionsManager, textDocument.FilePath); } InitializeGuidelines(settings.GuideLinePositionsInChars); _view.LayoutChanged += OnViewLayoutChanged; _settingsChanged = settings as INotifyPropertyChanged; if (_settingsChanged != null) { _settingsChanged.PropertyChanged += SettingsChanged; } _view.Closed += ViewClosed; }
private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, Workspace workspace, ICodingConventionsManager codingConventionsManager) { var tree = context.Tree; var cancellationToken = context.CancellationToken; var options = workspace.Options; if (File.Exists(tree.FilePath)) { var codingConventionContext = codingConventionsManager.GetConventionContextAsync(tree.FilePath, cancellationToken).GetAwaiter().GetResult(); options = ApplyFormattingOptions(options, codingConventionContext); } FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, _descriptor, workspace, options); }
private static async Task <Solution> FormatFilesInProjectAsync(ILogger logger, Project project, ICodingConventionsManager codingConventionsManager, EditorConfigOptionsApplier optionsApplier, CancellationToken cancellationToken) { var isCommentTrivia = project.Language == LanguageNames.CSharp ? IsCSharpCommentTrivia : IsVisualBasicCommentTrivia; var formattedDocuments = new List <(DocumentId documentId, Task <SourceText> formatTask)>(); foreach (var documentId in project.DocumentIds) { var document = project.Solution.GetDocument(documentId); if (!document.SupportsSyntaxTree) { continue; } var formatTask = Task.Run(async() => { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (GeneratedCodeUtilities.IsGeneratedCode(syntaxTree, isCommentTrivia, cancellationToken)) { return(null); } logger.LogTrace(Resources.Formatting_code_file_0, Path.GetFileName(document.FilePath)); OptionSet documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var codingConventionsContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath, cancellationToken).ConfigureAwait(false); if (codingConventionsContext?.CurrentConventions != null) { documentOptions = optionsApplier.ApplyConventions(documentOptions, codingConventionsContext.CurrentConventions, project.Language); } var formattedDocument = await Formatter.FormatAsync(document, documentOptions, cancellationToken).ConfigureAwait(false); return(await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false)); }, cancellationToken); formattedDocuments.Add((documentId, formatTask)); } var formattedSolution = project.Solution; foreach (var(documentId, formatTask) in formattedDocuments) { var text = await formatTask.ConfigureAwait(false); if (text is null) { continue; } formattedSolution = formattedSolution.WithDocumentText(documentId, text); } return(formattedSolution); }
private static async Task <int> FormatFilesInSolutionAsync(ILogger logger, Solution solution, string projectPath, ICodingConventionsManager codingConventionsManager, CancellationToken cancellationToken) { var formattedSolution = solution; var optionsApplier = new EditorConfigOptionsApplier(); foreach (var projectId in formattedSolution.ProjectIds) { var project = formattedSolution.GetProject(projectId); if (!string.IsNullOrEmpty(projectPath) && !project.FilePath.Equals(projectPath, StringComparison.OrdinalIgnoreCase)) { logger.LogDebug(Resources.Skipping_referenced_project_0, project.Name); continue; } if (project.Language != LanguageNames.CSharp && project.Language != LanguageNames.VisualBasic) { logger.LogWarning(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, project.FilePath); continue; } logger.LogInformation(Resources.Formatting_code_files_in_project_0, project.Name); formattedSolution = await FormatFilesInProjectAsync(logger, project, codingConventionsManager, optionsApplier, cancellationToken).ConfigureAwait(false); } if (!solution.Workspace.TryApplyChanges(formattedSolution)) { logger.LogError(Resources.Failed_to_save_formatting_changes); return(1); } return(0); }
private static async Task <(Solution solution, int filesFormatted)> FormatFilesInProjectAsync(Project project, ICodingConventionsManager codingConventionsManager, EditorConfigOptionsApplier optionsApplier, ImmutableHashSet <string> filesToFormat, ILogger logger, CancellationToken cancellationToken) { var formattedDocuments = new List <(DocumentId documentId, Task <SourceText> formatTask)>(); foreach (var documentId in project.DocumentIds) { var document = project.Solution.GetDocument(documentId); if (!document.SupportsSyntaxTree) { continue; } if (!filesToFormat.IsEmpty && !filesToFormat.Contains(document.FilePath)) { continue; } var formatTask = Task.Run(async() => { if (await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken)) { return(null); } logger.LogTrace(Resources.Formatting_code_file_0, Path.GetFileName(document.FilePath)); OptionSet documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var codingConventionsContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath, cancellationToken).ConfigureAwait(false); if (codingConventionsContext?.CurrentConventions != null) { documentOptions = optionsApplier.ApplyConventions(documentOptions, codingConventionsContext.CurrentConventions, project.Language); } var formattedDocument = await Formatter.FormatAsync(document, documentOptions, cancellationToken).ConfigureAwait(false); var formattedSourceText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); if (formattedSourceText.ContentEquals(await document.GetTextAsync(cancellationToken).ConfigureAwait(false))) { // Avoid touching files that didn't actually change return(null); } logger.LogInformation(Resources.Formatted_code_file_0, Path.GetFileName(document.FilePath)); return(formattedSourceText); }, cancellationToken); formattedDocuments.Add((documentId, formatTask)); } var formattedSolution = project.Solution; var filesFormatted = 0; foreach (var(documentId, formatTask) in formattedDocuments) { var text = await formatTask.ConfigureAwait(false); if (text is null) { continue; } filesFormatted++; formattedSolution = formattedSolution.WithDocumentText(documentId, text); } return(formattedSolution, filesFormatted); }