Skip to content

Commit

Permalink
Fix VS Analyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlacey committed May 22, 2024
1 parent 7047f81 commit 8af05fa
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions VSIX/RapidXamlToolkit/Analysis/ErrorList/TableDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class TableDataSource : ITableDataSource
private TableDataSource()
{
#if VSIXNOTEXE
ThreadHelper.ThrowIfNotOnUIThread();

var compositionService = ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)) as IComponentModel;
compositionService?.DefaultCompositionService.SatisfyImportsOnce(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint d
public int OnAfterSave(uint docCookie)
{
#if VSIXNOTEXE
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var documentInfo = this.runningDocumentTable.GetDocumentInfo(docCookie);

var documentPath = documentInfo.Moniker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ private void OnXamlDocParsed(object sender, RapidXamlParsingEventArgs e)
private string GetProjectName(string fileName)
{
#if VSIXNOTEXE
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

try
{
return ProjectHelpers.Dte2.Solution.FindProjectItem(fileName)?.ContainingProject?.Name ?? string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public SuggestedActionsSource(IViewTagAggregatorFactoryService tagService, ISugg
this.view = view;
this.file = file;

Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

RapidXamlDocumentCache.Add(this.file, textBuffer.CurrentSnapshot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal sealed class SymbolIconAdornment : TextBlock

public SymbolIconAdornment(SymbolIconTag tag)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

this.Foreground = TextColor;
this.SymbolTag = tag;
this.Height = this.GetFontSize() + 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ protected override SymbolIconAdornment CreateAdornment(SymbolIconTag dataTag, Sn

protected override bool UpdateAdornment(SymbolIconAdornment adornment, SymbolIconTag dataTag)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

adornment.Update(dataTag);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public async Task<ParserOutput> GetXamlAsync(IAsyncServiceProvider serviceProvid

if (CodeParserBase.GetSettings().Profiles.Any())
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

if (!(await serviceProvider.GetServiceAsync(typeof(EnvDTE.DTE)) is DTE dte))
{
RapidXamlPackage.Logger?.RecordError(StringRes.Error_FailedToGetDteInGetXamlAsync);
Expand Down Expand Up @@ -96,6 +98,8 @@ public async Task<ParserOutput> GetXamlAsync(IAsyncServiceProvider serviceProvid

protected static async Task ShowStatusBarMessageAsync(IAsyncServiceProvider serviceProvider, string message)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

try
{
if (await serviceProvider.GetServiceAsync(typeof(EnvDTE.DTE)) is DTE dte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ private static IEnumerable<string> EnumerateDroppedFiles(MemoryStream stream)

private string GetFilename(DragDropInfo info)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (info.Data.GetData("CF_VSSTGPROJECTITEMS") is MemoryStream stream)
{
var tryCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static async Task InitializeAsync(AsyncPackage package, ILogger logger)

public IDropHandler GetAssociatedDropHandler(IWpfTextView view)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

try
{
ITextBufferUndoManager undoManager = this.UndoProvider.GetTextBufferUndoManager(view.TextBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public ConfiguredSettings(IServiceProvider vsServiceProvider)
{
try
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var shellSettingsManager = new ShellSettingsManager(vsServiceProvider);
this.store = shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

Expand Down
2 changes: 2 additions & 0 deletions VSIX/RapidXamlToolkit/Logging/RxtLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void RecordError(string message, Dictionary<string, string> properties =

public void RecordGeneralError(string message)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

GeneralOutputPane.Instance.Write($"[{StringRes.RapidXamlToolkit}] {message}");
GeneralOutputPane.Instance.Activate();
}
Expand Down
2 changes: 2 additions & 0 deletions VSIX/RapidXamlToolkit/Logging/RxtOutputPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private RxtOutputPane()

public static bool IsInitialized()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (ServiceProvider.GlobalProvider.GetService(typeof(SVsOutputWindow)) is IVsOutputWindow outWindow)
{
outWindow.GetPane(ref rxtPaneGuid, out IVsOutputWindowPane pane);
Expand Down
3 changes: 2 additions & 1 deletion VSIX/RapidXamlToolkit/ProjectHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.Operations;

namespace RapidXamlToolkit
{
public static class ProjectHelpers
{
static ProjectHelpers()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

Dte = (DTE)Package.GetGlobalService(typeof(DTE));
Dte2 = (DTE2)Package.GetGlobalService(typeof(DTE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static class SolutionExtensions

public static IEnumerable<EnvDTE.Project> GetAllProjects(this EnvDTE.Solution solution)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var item = solution.Projects.GetEnumerator();

while (item.MoveNext())
Expand All @@ -37,6 +39,8 @@ public static class SolutionExtensions

public static IEnumerable<EnvDTE.Project> GetSolutionFolderProjects(this EnvDTE.Project solutionFolder)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

for (var i = 1; i <= solutionFolder.ProjectItems.Count; i++)
{
var subProject = solutionFolder.ProjectItems.Item(i).SubProject;
Expand All @@ -62,6 +66,8 @@ public static class SolutionExtensions

public static EnvDTE.Project GetProjectContainingFile(this EnvDTE.Solution solution, string filePath)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

return solution.FindProjectItem(filePath).ContainingProject;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ public VisualStudioAbstraction(ILogger logger, IAsyncServiceProvider serviceProv

public string GetActiveDocumentFilePath()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

return this.Dte.ActiveDocument.FullName;
}

public ProjectType GetProjectType(EnvDTE.Project project)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

const string WpfGuid = "{60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}";
const string UwpGuid = "{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A}";
const string XamAndroidGuid = "{EFBA0AD7-5A72-4C68-AF49-83D382785DCF}";
Expand Down Expand Up @@ -253,6 +257,8 @@ ProjectType GetProjectTypeOfReferencingProject(EnvDTE.Project proj)

public bool ProjectUsesWpf(EnvDTE.Project project)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var rawContent = System.IO.File.ReadAllText(project.FullName);

return this.ProjectUsesWpf(rawContent);
Expand Down Expand Up @@ -285,6 +291,8 @@ public string GetProjectTypeGuids(EnvDTE.Project proj)
{
string projectTypeGuids = string.Empty;

Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

try
{
object service = this.GetService(proj.DTE, typeof(IVsSolution));
Expand All @@ -309,6 +317,8 @@ public string GetProjectTypeGuids(EnvDTE.Project proj)

public Guid GetProjectGuid(EnvDTE.Project proj)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var projectGuid = Guid.Empty;

try
Expand Down Expand Up @@ -339,6 +349,8 @@ public object GetService(object serviceProvider, Type type)

public object GetService(object serviceProviderObject, Guid guid)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

object service = null;

Microsoft.VisualStudio.OLE.Interop.IServiceProvider provider = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)serviceProviderObject;
Expand All @@ -361,6 +373,8 @@ public object GetService(object serviceProviderObject, Guid guid)

public string GetActiveDocumentText()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var activeDoc = this.Dte.ActiveDocument;

if (activeDoc.Object("TextDocument") is EnvDTE.TextDocument objectDoc)
Expand Down Expand Up @@ -414,6 +428,8 @@ public bool UserConfirms(string title, string message)

public (int Position, int LineNo) GetCursorPositionAndLineNumber()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var offset = ((TextSelection)this.Dte.ActiveDocument.Selection).AnchorPoint.AbsoluteCharOffset;
var lineNo = ((TextSelection)this.Dte.ActiveDocument.Selection).CurrentLine;

Expand Down Expand Up @@ -452,17 +468,23 @@ public async Task<int> GetXamlIndentAsync()

public string GetPathOfProjectContainingFile(string fileName)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

return this.Dte.Solution?.GetProjectContainingFile(fileName)?.FileName;
}

public (string ProjectFileName, ProjectType ProjectType) GetNameAndTypeOfProjectContainingFile(string fileName)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var proj = this.Dte.Solution.GetProjectContainingFile(fileName);
return (proj.FileName, this.GetProjectType(proj));
}

public string GetLanguageFromContainingProject(string fileName)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

try
{
var proj = ProjectHelpers.Dte.Solution.GetProjectContainingFile(fileName);
Expand Down Expand Up @@ -498,6 +520,8 @@ public string GetLanguageFromContainingProject(string fileName)

public List<string> GetFilesFromContainingProject(string fileName, params string[] fileNameEndings)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

var result = new List<string>();

// See also https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.shell.interop.ivssolution.getprojectenum?view=visualstudiosdk-2017#Microsoft_VisualStudio_Shell_Interop_IVsSolution_GetProjectEnum_System_UInt32_System_Guid__Microsoft_VisualStudio_Shell_Interop_IEnumHierarchies__
Expand Down Expand Up @@ -549,6 +573,8 @@ void IterateProject(EnvDTE.Project project)

private NuGet.VisualStudio.Contracts.INuGetProjectService GetNuGetService(EnvDTE.Project proj)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

// Cache these to avoid the overhead of looking them up multiple times for the same solution.
this.componentModel ??= this.GetService(proj.DTE, typeof(SComponentModel)) as IComponentModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void RemoveInActiveDocOnLine(string find, int lineNumber)

public void ReplaceInActiveDocOnLine(string find, string replace, int lineNumber)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
var matches = TextDocumentHelper.FindMatches(txtDoc, find);
Expand All @@ -47,6 +49,8 @@ public void ReplaceInActiveDocOnLine(string find, string replace, int lineNumber

public void ReplaceInActiveDocOnLineOrAbove(string find, string replace, int lineNumber)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
var matches = TextDocumentHelper.FindMatches(txtDoc, find);
Expand Down Expand Up @@ -77,6 +81,8 @@ public void ReplaceInActiveDoc(string find, string replace, int startIndex, int

public void ReplaceInActiveDoc(List<(string Find, string Replace)> replacements, int startIndex, int endIndex, Dictionary<int, int> exclusions = null)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
// Have to implement search and replace directly as built-in functionality doesn't provide the control to only replace within the desired area
Expand Down Expand Up @@ -116,6 +122,8 @@ public void ReplaceInActiveDoc(List<(string Find, string Replace)> replacements,

public void InsertIntoActiveDocumentOnNextLine(string text, int pos)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
txtDoc.Selection.MoveToAbsoluteOffset(pos);
Expand All @@ -127,11 +135,17 @@ public void InsertIntoActiveDocumentOnNextLine(string text, int pos)

public void InsertIntoActiveDocOnLineAfterClosingTag(int openingAngleBracketLineNumber, string toInsert)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is TextDocument txtDoc)
{
var matches = TextDocumentHelper.FindMatches(txtDoc, ">");

var matchPoint = matches.FirstOrDefault(m => m.Line >= openingAngleBracketLineNumber);
var matchPoint = matches.FirstOrDefault(m =>
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
return m.Line >= openingAngleBracketLineNumber;
});

if (matchPoint is not null)
{
Expand All @@ -144,6 +158,8 @@ public void InsertIntoActiveDocOnLineAfterClosingTag(int openingAngleBracketLine
// Track the return value to know whether to end/close the UndoContext.
public bool StartSingleUndoOperation(string name)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (!this.Dte.UndoContext.IsOpen)
{
this.Dte.UndoContext.Open(name);
Expand All @@ -155,11 +171,15 @@ public bool StartSingleUndoOperation(string name)

public void EndSingleUndoOperation()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

this.Dte.UndoContext.Close();
}

public void InsertAtEndOfLine(int lineNumber, string toInsert)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
txtDoc.Selection.MoveToLineAndOffset(lineNumber, 1);
Expand All @@ -170,6 +190,8 @@ public void InsertAtEndOfLine(int lineNumber, string toInsert)

public void DeleteFromEndOfLine(int lineNumber, int charsToDelete)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
txtDoc.Selection.MoveToLineAndOffset(lineNumber, 1);
Expand All @@ -180,6 +202,8 @@ public void DeleteFromEndOfLine(int lineNumber, int charsToDelete)

public void AddXmlnsAliasToActiveDoc(string alias, string value)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

if (this.Dte.ActiveDocument.Object("TextDocument") is EnvDTE.TextDocument txtDoc)
{
var matches = TextDocumentHelper.FindMatches(txtDoc, ">");
Expand Down

0 comments on commit 8af05fa

Please sign in to comment.