Get Code Formattig rules #475
-
I use the following code to get the indent size, but it ignores What am I doing wrong? var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
using var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
// Get the formatting rules for the C# language
var indentationSize = workspace.Options.GetOption(FormattingOptions.IndentationSize, LanguageNames.CSharp); I call the above code in a command |
Beta Was this translation helpful? Give feedback.
Answered by
reduckted
Dec 20, 2023
Replies: 1 comment 1 reply
-
Because the options in the VisualStudioWorkspace workspace = await VS.GetMefServiceAsync<VisualStudioWorkspace>();
// We need a file name. For this example, I'm just using the active document,
// but you can get the file name from somewhere else if you want.
DocumentView documentView = await VS.Documents.GetActiveDocumentViewAsync();
if (documentView is not null)
{
// Find the document in the workspace.
DocumentId documentId = workspace
.CurrentSolution
.GetDocumentIdsWithFilePath(documentView.FilePath)
.FirstOrDefault();
if (documentId is not null)
{
// Get the options from the document.
DocumentOptionSet options = await workspace.CurrentSolution.GetDocument(documentId).GetOptionsAsync();
// Get the value from the document's options.
int indentationSize = options.GetOption(FormattingOptions.IndentationSize);
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
workgroupengineering
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because the options in the
.editorconfig
file can be set to different values for different types of files, you need to get the options from a document.