-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
296 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using EnvDTE; | ||
|
||
namespace Shared | ||
{ | ||
public interface ITextReplacer | ||
{ | ||
void Replace(string whatRegex, string with, Document document); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using EnvDTE; | ||
using EnvDTE80; | ||
using Shared; | ||
|
||
namespace WhitespaceCleanerExtension | ||
{ | ||
public sealed class TextReplacer : ITextReplacer | ||
{ | ||
private DTE2 _applicationObject; | ||
|
||
public TextReplacer(DTE2 applicationObject) | ||
{ | ||
_applicationObject = applicationObject; | ||
} | ||
|
||
public void Replace(string whatRegex, string with, Document document) | ||
{ | ||
_applicationObject.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, whatRegex, | ||
(int)vsFindOptions.vsFindOptionsRegularExpression, | ||
with, | ||
vsFindTarget.vsFindTargetCurrentDocument, string.Empty, string.Empty, | ||
vsFindResultsLocation.vsFindResultsNone); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("WhitespaceCleanerExtension2022")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("WhitespaceCleanerExtension2022")] | ||
[assembly: AssemblyCopyright("[email protected]")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(true)] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.4")] | ||
[assembly: AssemblyFileVersion("1.0.0.4")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using EnvDTE; | ||
using Microsoft.VisualStudio.Editor; | ||
using Microsoft.VisualStudio.Shell; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Operations; | ||
using Microsoft.VisualStudio.TextManager.Interop; | ||
using Shared; | ||
using System; | ||
using WhitespaceCleanerExtension; | ||
|
||
namespace WhitespaceCleanerExtension2022 | ||
{ | ||
public sealed class TextReplacer : ITextReplacer | ||
{ | ||
public void Replace(string whatRegex, string with, Document document) | ||
{ | ||
ThreadHelper.ThrowIfNotOnUIThread(); | ||
|
||
if (TryGetTextBufferAt(document.FullName, out var textBuffer)) | ||
{ | ||
var findService = SaveCommandPackage.Instance.ComponentModel.GetService<IFindService>(); | ||
|
||
var finderFactory = findService.CreateFinderFactory(whatRegex, with, FindOptions.UseRegularExpressions); | ||
var finder = finderFactory.Create(textBuffer.CurrentSnapshot); | ||
|
||
using (var edit = textBuffer.CreateEdit()) | ||
{ | ||
foreach (var match in finder.FindForReplaceAll()) | ||
edit.Replace(match.Match, match.Replace); | ||
|
||
edit.Apply(); | ||
} | ||
} | ||
} | ||
|
||
private static bool TryGetTextBufferAt(string filePath, out ITextBuffer textBuffer) | ||
{ | ||
if (VsShellUtilities.IsDocumentOpen(SaveCommandPackage.Instance, filePath, Guid.Empty, out var _, out var _, out var windowFrame)) | ||
{ | ||
IVsTextView view = VsShellUtilities.GetTextView(windowFrame); | ||
if (view.GetBuffer(out var lines) == 0) | ||
{ | ||
if (lines is IVsTextBuffer buffer) | ||
{ | ||
var editorAdapterFactoryService = SaveCommandPackage.Instance.ComponentModel.GetService<IVsEditorAdaptersFactoryService>(); | ||
textBuffer = editorAdapterFactoryService.GetDataBuffer(buffer); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
textBuffer = null; | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.