@@ -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 {
0 commit comments