@@ -37,7 +37,7 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
3737 allResults = ( await this . getTagNameCompletions ( document , position , token ) ) . concat ( this . getClosingTagNameCompletions ( document , position ) ) ;
3838 }
3939 else if ( isAttributeValue ( document , position ) ) {
40- allResults = this . getAttributeValueCompletions ( document , position , token ) ;
40+ allResults = await this . getAttributeValueCompletions ( document , position , token ) ;
4141 }
4242 else if ( isAttribute ( document , position ) ) {
4343 allResults = await this . getAttributeCompletions ( document , position , token ) ;
@@ -129,9 +129,26 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
129129 return await this . getAttributeCompletionsForElement ( tag , false , document , position , token ) ;
130130 }
131131
132+ private hasUnnamedArgs ( tag : string ) : boolean {
133+ return tag === 'Text' ;
134+ }
135+
136+ private getUnnamedArgsRange ( tag : string , dartDocument : TextDocument , offset : number ) : number {
137+ const dart = dartDocument . getText ( ) ;
138+ offset = dart . indexOf ( ' ' + tag + '(' , offset - tag . length - 4 ) ;
139+ const pos = dartDocument . positionAt ( offset ) . translate ( { lineDelta : 1 , characterDelta : 200 } ) ;
140+ offset = dartDocument . offsetAt ( pos ) ;
141+ return offset ;
142+ }
143+
132144 private async getAttributeCompletionsForElement ( elementTag : string , isTag : boolean , xmlDocument : TextDocument , xmlPosition : Position , token : CancellationToken ) : Promise < CompletionItem [ ] > {
133145 const dartDocument = await getDartDocument ( xmlDocument ) ;
134- const dartOffset = await getDartCodeIndex ( xmlDocument , xmlPosition , dartDocument , null ) ;
146+ const hasUnnamedArgs = this . hasUnnamedArgs ( elementTag ) ;
147+ const wordRange = xmlDocument . getWordRangeAtPosition ( xmlPosition ) ;
148+ let dartOffset = getDartCodeIndex ( xmlDocument , xmlPosition , dartDocument , wordRange , false , ! ! elementTag , hasUnnamedArgs ) ;
149+ if ( hasUnnamedArgs ) {
150+ dartOffset = this . getUnnamedArgsRange ( elementTag , dartDocument , dartOffset ) ;
151+ }
135152
136153 const line = xmlDocument . lineAt ( xmlPosition . line ) . text . slice ( 0 , xmlPosition . character ) ;
137154 const nextCharacter = xmlDocument . getText ( new Range ( xmlPosition , xmlPosition . translate ( { characterDelta : 200 } ) ) ) . trim ( ) . substr ( 0 , 1 ) ;
@@ -143,13 +160,8 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
143160 offset : dartOffset ,
144161 } ) ;
145162
146- // get used attributes
147- // todo
148- const currentUsedAttributes : string [ ] = [ ] ;
149-
150163 // map results
151164 const includedResults = resp . results
152- . filter ( ( a : any ) => currentUsedAttributes . indexOf ( a . displayText || a . completion ) === - 1 )
153165 . map ( ( r : any ) => {
154166 const a = this . convertResult ( xmlDocument , nextCharacter , enableCommitCharacters , insertArgumentPlaceholders , resp , r ) ;
155167 const item = new vs . CompletionItem ( a . label . replace ( ':' , '' ) , a . kind ) ;
@@ -168,7 +180,7 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
168180 } ) ;
169181
170182 const cachedResults = await this . getCachedResults ( xmlDocument , token , nextCharacter , enableCommitCharacters , insertArgumentPlaceholders , xmlDocument . offsetAt ( xmlPosition ) , resp ) ;
171- return [ ...includedResults , ...cachedResults , ...( isTag ? this . getWrapperPropertiesElementsCompletionItems ( xmlDocument , xmlPosition ) : this . getWrapperPropertiesCompletionItems ( xmlDocument , xmlPosition ) ) ] ;
183+ return [ ...includedResults , ...( cachedResults && cachedResults . length ? cachedResults : [ ] ) , ...( isTag ? this . getWrapperPropertiesElementsCompletionItems ( xmlDocument , xmlPosition ) : this . getWrapperPropertiesCompletionItems ( xmlDocument , xmlPosition ) ) ] ;
172184 }
173185
174186 private getAttributeCompletionsForAttribute ( elementTag : string , document : TextDocument , position : Position ) : vs . CompletionItem [ ] {
@@ -267,13 +279,13 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
267279 return item ;
268280 }
269281
270- private getAttributeValueCompletions ( document : TextDocument , position : Position , token : CancellationToken ) : CompletionItem [ ] {
271- const completions : CompletionItem [ ] = [ ] ;
272-
282+ private async getAttributeValueCompletions ( xmlDocument : TextDocument , xmlPosition : Position , token : CancellationToken ) : Promise < CompletionItem [ ] > {
273283 // Get the attribute name
274- const wordRange = document . getWordRangeAtPosition ( position ) ;
275- const wordStart = wordRange ? wordRange . start : position ;
276- const line = document . getText ( new Range ( wordStart . line , 0 , wordStart . line , wordStart . character ) ) ;
284+ const wordRange = xmlDocument . getWordRangeAtPosition ( xmlPosition ) ;
285+ const wordStart = wordRange ? wordRange . start : xmlPosition ;
286+ const wordEnd = wordRange ? wordRange . end : xmlPosition ;
287+ const lineRange = new Range ( wordStart . line , 0 , wordStart . line , wordEnd . character ) ;
288+ const line = xmlDocument . getText ( lineRange ) ;
277289 const attrNamePattern = / [ \. \- : _ a - z A - Z 0 - 9 ] + = / g;
278290 const match = line . match ( attrNamePattern ) ;
279291
@@ -285,7 +297,7 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
285297 attrName = attrName . slice ( 0 , - 1 ) ;
286298
287299 // Get the XPath
288- const xPath = getXPath ( document , position ) ;
300+ const xPath = getXPath ( xmlDocument , xmlPosition ) ;
289301 const isTopLevelElement = xPath . length === 1 ;
290302 const isSecondLevelElement = xPath . length === 2 ;
291303
@@ -300,15 +312,37 @@ export class DartCompletionItemProvider implements CompletionItemProvider, IAmDi
300312 }
301313 }
302314
303- // todo
304- const children : any [ ] = [ ] ;
315+ const dartDocument = await getDartDocument ( xmlDocument ) ;
316+ let attrValue = xmlDocument . getText ( new Range ( new Position ( xmlPosition . line , 0 ) , xmlPosition . translate ( { characterDelta : 200 } ) ) ) ;
317+ let endIndex = attrValue . indexOf ( '" ' ) ;
318+ endIndex = endIndex === - 1 ? attrValue . indexOf ( '">' ) : endIndex ;
319+ attrValue = attrValue . substring ( attrValue . indexOf ( attrName + '="' ) + attrName . length + 2 , endIndex ) ;
320+ const cursorPos = line . substr ( line . lastIndexOf ( attrName + '="' ) + 2 ) . length - attrName . length - Math . abs ( wordEnd . character - xmlPosition . character ) ;
321+ const dartOffset = dartDocument . getText ( ) . indexOf ( attrName + ': ' + attrValue ) + attrName . length + 2 + cursorPos ;
322+ const nextCharacter = xmlDocument . getText ( new Range ( xmlPosition , xmlPosition . translate ( { characterDelta : 200 } ) ) ) . trim ( ) . substr ( 0 , 1 ) ;
323+ const conf = config . for ( xmlDocument . uri ) ;
324+ const enableCommitCharacters = conf . enableCompletionCommitCharacters ;
325+ const insertArgumentPlaceholders = ! enableCommitCharacters && conf . insertArgumentPlaceholders && this . shouldAllowArgPlaceholders ( line ) ;
305326
306- // Apply a filter with the current prefix and return.
307- children . forEach ( ( child : any ) => {
308- const suggestion = new CompletionItem ( child . displayText ) ;
309- suggestion . detail = child . rightLabel ;
310- completions . push ( suggestion ) ;
327+ const resp = await this . analyzer . completionGetSuggestionsResults ( {
328+ file : fsPath ( dartDocument . uri ) ,
329+ offset : dartOffset ,
311330 } ) ;
331+
332+ // todo
333+ const completions : CompletionItem [ ] = resp . results
334+ . map ( ( r : any ) => {
335+ const a = this . convertResult ( xmlDocument , nextCharacter , enableCommitCharacters , insertArgumentPlaceholders , resp , r ) ;
336+ const item = new vs . CompletionItem ( a . label . replace ( ':' , '' ) , a . kind ) ;
337+ ( item as any ) . _documentation = ( a as any ) . _documentation ;
338+ // item.insertText = new SnippetString(item.label + '>$0</' + item.label + '>');
339+ // item.insertText = new SnippetString(item.label + '>$0');
340+ // item.insertText = new SnippetString(item.label + '="$0"');
341+ item . detail = a . detail ;
342+ item . filterText = a . filterText ;
343+ item . sortText = a . sortText ;
344+ return item ;
345+ } ) ;
312346
313347 return completions ;
314348 }
0 commit comments