private Task UpdateOptionsFromEditorConfigAsync(object sender, CodingConventionsChangedEventArgs args) { if (editorConfigContext == null) { return(Task.FromResult(false)); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetIndentStyle(out var indentStyle)) { EditorOptions.SetOptionValue(DefaultOptions.ConvertTabsToSpacesOptionName, indentStyle == IndentStyle.Spaces); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetTabWidth(out var tabWidth)) { EditorOptions.SetOptionValue(DefaultOptions.TabSizeOptionName, tabWidth); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetIndentSize(out var indentSize)) { EditorOptions.SetOptionValue(DefaultOptions.IndentSizeOptionName, indentSize); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetLineEnding(out var lineEnding)) { EditorOptions.SetOptionValue(DefaultOptions.NewLineCharacterOptionName, lineEnding); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetAllowTrailingWhitespace(out var allowTrailingWhitespace)) { EditorOptions.SetOptionValue(DefaultOptions.TrimTrailingWhiteSpaceOptionName, !allowTrailingWhitespace); } return(Task.FromResult(true)); }
private Task UpdateContextOptions(object sender, CodingConventionsChangedEventArgs arg) { if (context == null) { return(Task.FromResult(false)); } bool followCodingConventions = IdeApp.Preferences.Editor.FollowCodingConventions; defaultEolMarkerFromContext = null; if (followCodingConventions && context.CurrentConventions.UniversalConventions.TryGetLineEnding(out string eolMarker)) { defaultEolMarkerFromContext = eolMarker; } tabsToSpacesFromContext = null; if (followCodingConventions && context.CurrentConventions.UniversalConventions.TryGetIndentStyle(out Microsoft.VisualStudio.CodingConventions.IndentStyle result)) { tabsToSpacesFromContext = result == Microsoft.VisualStudio.CodingConventions.IndentStyle.Spaces; } indentationSizeFromContext = null; if (followCodingConventions && context.CurrentConventions.UniversalConventions.TryGetIndentSize(out int indentSize)) { indentationSizeFromContext = indentSize; } removeTrailingWhitespacesFromContext = null; if (followCodingConventions && context.CurrentConventions.UniversalConventions.TryGetAllowTrailingWhitespace(out bool allowTrailing)) { removeTrailingWhitespacesFromContext = !allowTrailing; } tabSizeFromContext = null; if (followCodingConventions && context.CurrentConventions.UniversalConventions.TryGetTabWidth(out int tSize)) { tabSizeFromContext = tSize; } rulerColumnFromContext = null; showRulerFromContext = null; if (followCodingConventions && context.CurrentConventions.TryGetConventionValue <string> (EditorConfigService.MaxLineLengthConvention, out string maxLineLength)) { if (maxLineLength != "off" && int.TryParse(maxLineLength, out int i)) { rulerColumnFromContext = i; showRulerFromContext = true; } else { showRulerFromContext = false; } } return(Task.FromResult(true)); }
private Task UpdateOptionsFromEditorConfigAsync(object sender, CodingConventionsChangedEventArgs args) { if (editorConfigContext == null) { return(Task.FromResult(false)); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetIndentStyle(out var indentStyle)) { EditorOptions.SetOptionValue(DefaultOptions.ConvertTabsToSpacesOptionName, indentStyle == IndentStyle.Spaces); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetTabWidth(out var tabWidth)) { EditorOptions.SetOptionValue(DefaultOptions.TabSizeOptionName, tabWidth); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetIndentSize(out var indentSize)) { EditorOptions.SetOptionValue(DefaultOptions.IndentSizeOptionName, indentSize); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetLineEnding(out var lineEnding)) { EditorOptions.SetOptionValue(DefaultOptions.NewLineCharacterOptionName, lineEnding); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetAllowTrailingWhitespace(out var allowTrailingWhitespace)) { EditorOptions.SetOptionValue(DefaultOptions.TrimTrailingWhiteSpaceOptionName, !allowTrailingWhitespace); } var setVerticalRulers = false; int [] verticalRulers = null; if (editorConfigContext.CurrentConventions.TryGetConventionValue <string> (EditorConfigService.RulersConvention, out var rulers)) { setVerticalRulers = true; if (!string.IsNullOrEmpty(rulers)) { verticalRulers = Array.ConvertAll(rulers.Split(','), val => { if (int.TryParse(val, out var col)) { return(col); } return(0); }); } } else if (editorConfigContext.CurrentConventions.TryGetConventionValue <string> (EditorConfigService.MaxLineLengthConvention, out var maxLineLength)) { if (maxLineLength != "off" && int.TryParse(maxLineLength, out var i)) { setVerticalRulers = true; verticalRulers = new [] { i }; } else { setVerticalRulers = false; } } if (setVerticalRulers) { EditorOptions.SetOptionValue(DefaultTextViewOptions.VerticalRulersName, verticalRulers ?? Array.Empty <int> ()); } return(Task.FromResult(true)); }
private Task OnCodingConventionsChangedAsync(object sender, CodingConventionsChangedEventArgs arg) => UpdateGuidelinesFromCodingConventionAsync((ICodingConventionContext)sender, _codingConventionsCancellationTokenSource.Token);
private Task UpdateOptionsFromEditorConfigAsync(object sender, CodingConventionsChangedEventArgs args) { // Set base options first, then override with editorconfig values UpdateOptionsFromPolicy(); if (editorConfigContext == null) { return(Task.CompletedTask); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetIndentStyle(out var indentStyle)) { SetOptionValue(DefaultOptions.ConvertTabsToSpacesOptionName, indentStyle == IndentStyle.Spaces); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetTabWidth(out var tabWidth)) { SetOptionValue(DefaultOptions.TabSizeOptionName, tabWidth); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetIndentSize(out var indentSize)) { SetOptionValue(DefaultOptions.IndentSizeOptionName, indentSize); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetLineEnding(out var lineEnding)) { SetOptionValue(DefaultOptions.NewLineCharacterOptionName, lineEnding); } if (editorConfigContext.CurrentConventions.UniversalConventions.TryGetAllowTrailingWhitespace(out var allowTrailingWhitespace)) { SetOptionValue(DefaultOptions.TrimTrailingWhiteSpaceOptionName, !allowTrailingWhitespace); } var setVerticalRulers = false; int [] verticalRulers = null; // "Show column ruler" preference still needs to be checked, regardless of whether or not editorconfig // has a setting for where rulers should appear if (PropertyService.Get <bool> ("ShowRuler")) { if (editorConfigContext.CurrentConventions.TryGetConventionValue <string> (EditorConfigService.RulersConvention, out var rulers)) { setVerticalRulers = true; if (!string.IsNullOrEmpty(rulers)) { verticalRulers = Array.ConvertAll(rulers.Split(','), val => { if (int.TryParse(val, out var col)) { return(col); } return(0); }); } } else if (editorConfigContext.CurrentConventions.TryGetConventionValue <string> (EditorConfigService.MaxLineLengthConvention, out var maxLineLength)) { if (maxLineLength != "off" && int.TryParse(maxLineLength, out var i)) { setVerticalRulers = true; verticalRulers = new [] { i }; } else { setVerticalRulers = false; } } } #if !WINDOWS if (setVerticalRulers) { EditorOptions.SetOptionValue(DefaultTextViewOptions.VerticalRulersName, verticalRulers ?? Array.Empty <int> ()); } #endif return(Task.CompletedTask); }
private Task HandleCodingConventionsChangedAsync(DocumentId documentId, object sender, CodingConventionsChangedEventArgs e) { var projectId = documentId.ProjectId; var projectsAlreadyNotified = s_projectNotifications.GetOrCreateValue(e); lock (projectsAlreadyNotified) { if (!projectsAlreadyNotified.Add(projectId)) { return(Task.CompletedTask); } } var diagnosticAnalyzerService = ((IMefHostExportProvider)_workspace.Services.HostServices).GetExports <IDiagnosticAnalyzerService>().Single().Value; var foregroundNotificationService = ((IMefHostExportProvider)_workspace.Services.HostServices).GetExports <IForegroundNotificationService>().Single().Value; foregroundNotificationService.RegisterNotification(UpdateProject, _listener.BeginAsyncOperation(nameof(HandleCodingConventionsChangedAsync)), CancellationToken.None); return(Task.CompletedTask); void UpdateProject() { if (!_workspace.CurrentSolution.ContainsProject(projectId)) { // The project or solution was closed before running this update. return; } // Send a notification that project options have changed. This ensures the options used by commands, // e.g. Format Document, are correct following a change to .editorconfig. Unlike a change to compilation // or parse options, this does not discard the syntax and semantics already gathered for the solution. _workspace.OnProjectOptionsChanged(projectId); // Request diagnostics be run on the project again. diagnosticAnalyzerService.Reanalyze(_workspace, SpecializedCollections.SingletonEnumerable(projectId)); } }