Skip to content

Commit 47aa98a

Browse files
committed
- Improve OpenFile method
1 parent 7bfe330 commit 47aa98a

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

Codist/Helpers/TextEditorHelper.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,30 @@ public static void OpenFile(string file, Action<VsTextView> action, bool newWind
555555
static void InternalOpenFile(string file, Action<VsTextView> action, bool newWindow = false, bool useDesigner = false) {
556556
try {
557557
using (new NewDocumentStateScope(newWindow ^ UIHelper.IsShiftDown, VSConstants.NewDocumentStateReason.Navigation)) {
558-
VsShellUtilities.OpenDocument(ServiceProvider.GlobalProvider,
558+
Guid editorType;
559+
Guid logicalView;
560+
if (!useDesigner) {
561+
editorType = VSConstants.VsEditorFactoryGuid.TextEditor_guid;
562+
logicalView = __ViewKindCodeGuid;
563+
}
564+
else {
565+
Guid std = GetStandardEditorGuidForFile(file, __ViewKindPrimaryGuid);
566+
editorType = (std != Guid.Empty) ? std : VSConstants.VsEditorFactoryGuid.TextEditor_guid;
567+
logicalView = __ViewKindPrimaryGuid;
568+
}
569+
VsShellUtilities.OpenDocumentWithSpecificEditor(
570+
ServiceProvider.GlobalProvider,
559571
file,
560-
useDesigner ? __ViewKindPrimaryGuid : __ViewKindCodeGuid,
572+
editorType,
573+
logicalView,
561574
out var hierarchy,
562575
out var itemId,
563-
out var windowFrame,
564-
out var view);
565-
action?.Invoke(view);
576+
out var windowFrame);
577+
windowFrame.Show();
578+
var view = VsShellUtilities.GetTextView(windowFrame);
579+
if (view != null) {
580+
action?.Invoke(view);
581+
}
566582
}
567583
}
568584
catch (Exception ex) {
@@ -571,6 +587,15 @@ static void InternalOpenFile(string file, Action<VsTextView> action, bool newWin
571587
}
572588
}
573589

590+
[SuppressMessage("Usage", Suppression.VSTHRD010, Justification = Suppression.CheckedInCaller)]
591+
static Guid GetStandardEditorGuidForFile(string file, Guid logicalView) {
592+
var vsUiOpenDoc = ServicesHelper.Get<IVsUIShellOpenDocument, SVsUIShellOpenDocument>();
593+
Guid editorGuid = Guid.Empty;
594+
return vsUiOpenDoc != null && ErrorHandler.Succeeded(vsUiOpenDoc.GetStandardEditorFactory(0, ref editorGuid, file, ref logicalView, out _, out _))
595+
? editorGuid
596+
: Guid.Empty;
597+
}
598+
574599
static void MoveCaretToKeptViewPosition() {
575600
var v = __MouseOverDocumentView ?? __ActiveDocumentView;
576601
SnapshotPoint p;

0 commit comments

Comments
 (0)