@@ -7,35 +7,76 @@ import * as vscode from 'vscode'
77 * Context for Pine Inline Completion.
88 * Provides necessary information for generating inline completions in Pine Script.
99 */
10- export class PineInlineCompletionContext implements vscode . InlineCompletionItemProvider {
11- selectedCompletionText : string | undefined ;
12-
10+ export class PineInlineCompletionHandler {
1311 /**
14- * Provides inline completion items for the current position in the document.
15- * @param document - The current document.
16- * @param position - The current position within the document.
12+ * Handles the selection of an inline completion item.
1713 * @param context - The inline completion context.
18- * @returns null
1914 */
20- provideInlineCompletionItems ( document : vscode . TextDocument , position : vscode . Position , context : vscode . InlineCompletionContext ) : vscode . ProviderResult < vscode . InlineCompletionItem [ ] | vscode . InlineCompletionList > {
21- const selectedCompletionText = context . selectedCompletionInfo ?. text
15+ handleInlineCompletionSelection ( context : vscode . InlineCompletionContext ) : void {
16+ const selectedCompletionText = context . selectedCompletionInfo ?. text ;
2217
2318 if ( selectedCompletionText ) {
24- this . selectedCompletionText = selectedCompletionText
25- PineSharedCompletionState . setSelectedCompletion ( context . selectedCompletionInfo ?. text )
26- vscode . commands . executeCommand ( 'editor.action.triggerParameterHints' )
19+ PineSharedCompletionState . setSelectedCompletion ( selectedCompletionText ) ;
20+ vscode . commands . executeCommand ( 'editor.action.triggerParameterHints' ) ;
2721 }
22+ }
2823
29- // console.log(context.selectedCompletionInfo?.text, 'selectedCompletionInfo')
30- return null
24+ provideInlineCompletionItems (
25+ document : vscode . TextDocument ,
26+ position : vscode . Position ,
27+ context : vscode . InlineCompletionContext ,
28+ token : vscode . CancellationToken
29+ ) : vscode . ProviderResult < vscode . InlineCompletionItem [ ] | vscode . InlineCompletionList > {
30+ // Implement your inline completion logic here
31+ return [ ] ;
3132 }
3233
3334 /**
3435 * Clears the selected completion text.
3536 */
36- clearSelectedCompletion ( ) {
37- PineSharedCompletionState . setSelectedCompletion ( undefined )
37+ clearSelectedCompletion ( ) : void {
38+ PineSharedCompletionState . setSelectedCompletion ( undefined ) ;
3839 }
40+
41+ /**
42+ * Registers the completion handler.
43+ * @param context - The extension context.
44+ */
45+ public static register ( context : vscode . ExtensionContext ) : void {
46+ const completionHandler = new PineInlineCompletionHandler ( ) ;
47+
48+ context . subscriptions . push (
49+ vscode . window . onDidChangeTextEditorSelection ( event => {
50+ const editor = event . textEditor ;
51+ if ( editor ) {
52+ const position = editor . selection . active ;
53+ const { document} = editor ;
54+ const range = new vscode . Range ( position , position ) ;
55+ let selectedInfo = undefined ;
56+
57+ // Check if the selection change was triggered by an inline completion
58+ if ( event . kind === vscode . TextEditorSelectionChangeKind . Mouse ) {
59+ const lastCompletion = PineSharedCompletionState . getLastCompletion ( ) ;
60+ if ( lastCompletion && lastCompletion . range . contains ( position ) ) {
61+ selectedInfo = {
62+ range : lastCompletion . range ,
63+ text : lastCompletion . insertText as string
64+ } ;
65+ }
66+ }
67+
68+ const context : vscode . InlineCompletionContext = {
69+ selectedCompletionInfo : selectedInfo ,
70+ triggerKind : vscode . InlineCompletionTriggerKind . Automatic
71+ } ;
72+
73+ completionHandler . handleInlineCompletionSelection ( context ) ;
74+ }
75+ } )
76+ ) ;
77+ }
78+
79+
3980}
4081
4182export class PineCompletionProvider implements vscode . CompletionItemProvider {
@@ -161,9 +202,7 @@ export class PineCompletionProvider implements vscode.CompletionItemProvider {
161202 const wordBoundaryRegexArgs = / (?: \( | , ) ? \s * \b [ \w . ] + $ /
162203 const argStartMatch = wordBoundaryRegexArgs . exec ( textBeforeCursor )
163204 let argStart = argStartMatch ? position . character - argStartMatch [ 0 ] . length : position . character
164- if ( argStart < 0 ) {
165- argStart = 0
166- }
205+ argStart = Math . max ( argStart , 0 )
167206
168207 if ( ! PineSharedCompletionState . getIsLastArg ) {
169208 insertText += ''
@@ -178,9 +217,7 @@ export class PineCompletionProvider implements vscode.CompletionItemProvider {
178217 const wordBoundaryRegex = / \b [ \w . ] + $ /
179218 const wordStartMatch = wordBoundaryRegex . exec ( textBeforeCursor )
180219 let wordStart = wordStartMatch ? position . character - wordStartMatch [ 0 ] . length : position . character
181- if ( wordStart < 0 ) {
182- wordStart = 0
183- }
220+ wordStart = Math . max ( wordStart , 0 )
184221
185222 // Set the replacement range and insert text of the completion item
186223 completionItem . insertText = insertText
@@ -317,10 +354,14 @@ export class PineCompletionProvider implements vscode.CompletionItemProvider {
317354
318355 if ( foundIndex === - 1 ) {
319356 typoTrack ++ ;
320- if ( typoTrack > 1 ) break ;
357+ if ( typoTrack > 1 ) {
358+ break ;
359+ }
321360 } else if ( foundIndex !== matchIndex ) {
322361 minorTypoCount ++ ;
323- if ( minorTypoCount >= 3 ) break ;
362+ if ( minorTypoCount >= 3 ) {
363+ break ;
364+ }
324365 matchIndex = foundIndex + 1 ;
325366 } else {
326367 matchIndex ++ ;
@@ -402,7 +443,7 @@ export class PineCompletionProvider implements vscode.CompletionItemProvider {
402443 'controls' ,
403444 'annotations' ,
404445 'fields' ,
405- 'fields2'
446+ 'fields2' ,
406447 ) ;
407448
408449 const lowerMatch = match . toLowerCase ( ) ;
@@ -421,10 +462,14 @@ export class PineCompletionProvider implements vscode.CompletionItemProvider {
421462
422463 if ( foundIndex === - 1 ) {
423464 majorTypoCount ++ ;
424- if ( majorTypoCount > 1 ) break ;
465+ if ( majorTypoCount > 1 ) {
466+ break ;
467+ }
425468 } else if ( foundIndex !== matchIndex ) {
426469 minorTypoCount ++ ;
427- if ( minorTypoCount >= 3 ) break ;
470+ if ( minorTypoCount >= 3 ) {
471+ break ;
472+ }
428473 matchIndex = foundIndex + 1 ;
429474 } else {
430475 matchIndex ++ ;
0 commit comments