@@ -32,60 +32,72 @@ public DocumentService(ILog log, IServiceProvider serviceProvider, IVsSolution v
3232 this . editorAdaptersFactoryService = editorAdaptersFactoryService ;
3333 }
3434
35- public bool ShowDocument ( string path , Range selection )
35+ private bool ForDocument ( string path , Func < IVsWindowFrame , IVsTextView , bool > action )
3636 {
3737 var result = ThreadHelper . JoinableTaskFactory . Run ( async delegate
3838 {
3939 await ThreadHelper . JoinableTaskFactory . SwitchToMainThreadAsync ( ) ;
4040
4141 var tryOpenResult = VsShellUtilities . TryOpenDocument ( serviceProvider , path , Guid . Empty , out _ , out _ , out IVsWindowFrame windowFrame ) ;
42- if ( tryOpenResult == VSConstants . S_OK )
42+ if ( tryOpenResult == VSConstants . S_OK && windowFrame != null )
4343 {
44- windowFrame ? . Show ( ) ;
45-
46- if ( selection != null && windowFrame != null )
47- {
48- var textView = GetVsTextView ( windowFrame ) ;
49- if ( textView != null )
50- {
51- textView . CenterLines ( selection . Start . Line , 0 ) ;
52- textView . SetSelection ( selection . Start . Line , selection . Start . Character , selection . End . Line , selection . End . Character ) ;
53- }
54- }
44+ var textView = GetVsTextView ( windowFrame ) ;
45+ if ( textView == null ) log . Warn ( $ "Cannot get VsTextView '{ path } '") ;
5546
56- return true ;
47+ return action ( windowFrame , textView ) ;
5748 }
58- else log . Error ( $ "Cannot show document '{ path } ' (error code: { tryOpenResult } )") ;
49+ else log . Error ( $ "Cannot open document '{ path } ' (error code: { tryOpenResult } )") ;
5950
6051 return false ;
6152 } ) ;
6253
6354 return result ;
6455 }
6556
57+ public bool ShowDocument ( string path , Range selection )
58+ {
59+ return ForDocument ( path , ( windowFrame , textView ) =>
60+ {
61+ windowFrame . Show ( ) ;
62+ if ( selection != null && textView != null )
63+ {
64+ textView . CenterLines ( selection . Start . Line , 0 ) ;
65+ textView . SetSelection ( selection . Start . Line , selection . Start . Character , selection . End . Line , selection . End . Character ) ;
66+ }
67+ return true ;
68+ } ) ;
69+ }
70+
6671 public bool SelectInDocument ( string path , Range selection )
6772 {
68- var result = ThreadHelper . JoinableTaskFactory . Run ( async delegate
73+ return ForDocument ( path , ( windowFrame , textView ) =>
6974 {
70- await ThreadHelper . JoinableTaskFactory . SwitchToMainThreadAsync ( ) ;
75+ if ( selection != null && textView != null )
76+ {
77+ textView . SetSelection ( selection . Start . Line , selection . Start . Character , selection . End . Line , selection . End . Character ) ;
78+ }
79+ return true ;
80+ } ) ;
81+ }
7182
72- var tryOpenResult = VsShellUtilities . TryOpenDocument ( serviceProvider , path , Guid . Empty , out _ , out _ , out IVsWindowFrame windowFrame ) ;
73- if ( tryOpenResult == VSConstants . S_OK )
83+ public bool RevealRangeInDocument ( string path , Range range )
84+ {
85+ return ForDocument ( path , ( windowFrame , textView ) =>
86+ {
87+ if ( textView != null && range != null )
7488 {
75- var textView = GetVsTextView ( windowFrame ) ;
76- if ( textView != null )
89+ var span = new TextSpan ( )
7790 {
78- textView . SetSelection ( selection . Start . Line , selection . Start . Character , selection . End . Line , selection . End . Character ) ;
79- }
80- else log . Error ( $ "Cannot get VsTextView '{ path } '") ;
91+ iStartLine = range . Start . Line ,
92+ iStartIndex = range . Start . Character ,
93+ iEndLine = range . End . Line ,
94+ iEndIndex = range . End . Character
95+ } ;
8196
97+ textView . EnsureSpanVisible ( span ) ;
8298 }
83- else log . Error ( $ "Cannot open document '{ path } ' (error code: { tryOpenResult } )") ;
84-
85- return false ;
99+ return true ;
86100 } ) ;
87-
88- return result ;
89101 }
90102
91103 public bool InsertTextInDocument ( string path , Position position , string text )
0 commit comments