@@ -96,109 +96,24 @@ public bool HasDesigner
9696 /// <param name="tempFile">True if a unique temp file name should be generated. WARNING: filenames generated with this flag are not persisted.</param>
9797 public string ExportAsSourceFile ( string folder , bool tempFile = false )
9898 {
99- var fullPath = tempFile
100- ? Path . Combine ( folder , Path . GetRandomFileName ( ) )
101- : Path . Combine ( folder , SafeName + Type . FileExtension ( ) ) ;
102- switch ( Type )
103- {
104- case ComponentType . UserForm :
105- ExportUserFormModule ( fullPath ) ;
106- break ;
107- case ComponentType . Document :
108- ExportDocumentModule ( fullPath ) ;
109- break ;
110- default :
111- Export ( fullPath ) ;
112- break ;
113- }
114-
115- return fullPath ;
99+ throw new NotSupportedException ( "Export as source file is not supported in VB6" ) ;
116100 }
117101
118102 public IVBProject ParentProject => Collection . Parent ;
119103
120- private void ExportUserFormModule ( string path )
121- {
122- // VBIDE API inserts an extra newline when exporting a UserForm module.
123- // this issue causes forms to always be treated as "modified" in source control, which causes conflicts.
124- // we need to remove the extra newline before the file gets written to its output location.
125-
126- int legitEmptyLineCount ;
127- using ( var codeModule = CodeModule )
128- {
129- var visibleCode = codeModule . Content ( ) . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
130- legitEmptyLineCount = visibleCode . TakeWhile ( string . IsNullOrWhiteSpace ) . Count ( ) ;
131- }
132-
133- var tempFile = ExportToTempFile ( ) ;
134- var tempFilePath = Directory . GetParent ( tempFile ) . FullName ;
135- var fileEncoding = System . Text . Encoding . Default ; //We use the current ANSI codepage because that is what the VBE does.
136- var contents = File . ReadAllLines ( tempFile , fileEncoding ) ;
137- var nonAttributeLines = contents . TakeWhile ( line => ! line . StartsWith ( "Attribute" ) ) . Count ( ) ;
138- var attributeLines = contents . Skip ( nonAttributeLines ) . TakeWhile ( line => line . StartsWith ( "Attribute" ) ) . Count ( ) ;
139- var declarationsStartLine = nonAttributeLines + attributeLines + 1 ;
140-
141- var emptyLineCount = contents . Skip ( declarationsStartLine - 1 )
142- . TakeWhile ( string . IsNullOrWhiteSpace )
143- . Count ( ) ;
144-
145- var code = contents ;
146- if ( emptyLineCount > legitEmptyLineCount )
147- {
148- code = contents . Take ( declarationsStartLine ) . Union (
149- contents . Skip ( declarationsStartLine + emptyLineCount - legitEmptyLineCount ) )
150- . ToArray ( ) ;
151- }
152- File . WriteAllLines ( path , code , fileEncoding ) ;
104+ public int FileCount => IsWrappingNullReference ? 0 : Target . FileCount ;
153105
154- // LINQ hates this search, therefore, iterate the long way
155- foreach ( string line in contents )
106+ public string GetFileName ( short index )
107+ {
108+ if ( IsWrappingNullReference )
156109 {
157- if ( line . Contains ( "OleObjectBlob" ) )
158- {
159- var binaryFileName = line . Trim ( ) . Split ( '"' ) [ 1 ] ;
160- var destPath = Directory . GetParent ( path ) . FullName ;
161- if ( File . Exists ( Path . Combine ( tempFilePath , binaryFileName ) ) && ! destPath . Equals ( tempFilePath ) )
162- {
163- System . Diagnostics . Debug . WriteLine ( Path . Combine ( destPath , binaryFileName ) ) ;
164- if ( File . Exists ( Path . Combine ( destPath , binaryFileName ) ) )
165- {
166- try
167- {
168- File . Delete ( Path . Combine ( destPath , binaryFileName ) ) ;
169- }
170- catch ( Exception )
171- {
172- // Meh?
173- }
174- }
175- File . Copy ( Path . Combine ( tempFilePath , binaryFileName ) , Path . Combine ( destPath , binaryFileName ) ) ;
176- }
177- break ;
178- }
110+ return null ;
179111 }
180- }
181-
182- private void ExportDocumentModule ( string path )
183- {
184- using ( var codeModule = CodeModule )
112+ if ( index < 1 || index > FileCount ) // 1-based indexing from VB
185113 {
186- var lineCount = codeModule . CountOfLines ;
187- if ( lineCount > 0 )
188- {
189- //One cannot reimport document modules as such in the VBE; so we simply export and import the contents of the code pane.
190- //Because of this, it is OK, and actually preferable, to use the default UTF8 encoding.
191- var text = codeModule . GetLines ( 1 , lineCount ) ;
192- File . WriteAllText ( path , text , Encoding . UTF8 ) ;
193- }
114+ throw new ArgumentOutOfRangeException ( nameof ( index ) ) ;
194115 }
195- }
196-
197- private string ExportToTempFile ( )
198- {
199- var path = Path . Combine ( Path . GetTempPath ( ) , SafeName + Type . FileExtension ( ) ) ;
200- Export ( path ) ;
201- return path ;
116+ return Target . FileNames [ index ] ;
202117 }
203118
204119 public override bool Equals ( ISafeComWrapper < VB . VBComponent > other )
0 commit comments