1- import { CompletionItem , CompletionItemKind , TextDocumentIdentifier , TextDocumentPositionParams } from 'vscode-languageserver' ;
2- import { Position , TextDocument } from 'vscode-languageserver-textdocument' ;
1+ import { CompletionItem , CompletionItemKind , Position , Range , TextDocumentIdentifier , TextDocumentPositionParams } from 'vscode-languageserver' ;
2+ import { TextDocument } from 'vscode-languageserver-textdocument' ;
33
44export default class CompletionService {
55
66 protected currentIndex : number = 0 ;
7- private lines : string [ ] = [ ] ;
87 private text : string = "" ;
8+ private offset :number = 0 ;
99 constructor ( public doc : TextDocument | undefined , public pos : Position ) {
10- if ( doc ) {
11- this . text = doc . getText ( ) ;
12- this . lines = this . text . split ( / \r ? \n / g) ;
13- }
10+ this . text = doc ?. getText ( ) || "" ;
11+ this . offset = doc ?. offsetAt ( pos ) || 0 ;
1412 }
1513
1614 getAllCompletions ( ) : CompletionItem [ ] {
15+ console . log ( "completion TRIGG" )
1716 let completions :CompletionItem [ ] = [ ] ;
17+
18+ //completions = completions.concat(this.getDotCompletions());
19+ //if(completions.length > 0) return completions;
20+ if ( this . isDot ( ) ) {
21+ return this . getDotCompletions ( ) ;
22+ }
23+
1824 completions = completions . concat ( this . getControlCompletions ( ) ) ;
1925 completions = completions . concat ( this . getGlobalFunctionCompletions ( ) ) ;
2026 completions = completions . concat ( this . getOutputCompletions ( ) ) ;
2127 completions = completions . concat ( this . getTypesCompletions ( ) ) ;
2228 completions = completions . concat ( this . getGlobalConstantsCompletions ( ) ) ;
23- completions = completions . concat ( this . getDotCompletions ( ) ) ;
2429
2530 return completions ;
2631 }
2732
2833 protected getCharRange ( begin :number , end :number ) {
2934 return this . text . substring ( begin , end ) ;
3035 }
36+
37+ protected isDot ( ) :boolean {
38+ const offset :number = this . doc ?. offsetAt ( this . pos ) || 0 ;
39+ const t = this . getCharRange ( offset - 1 , offset ) ;
40+ if ( t === '.' ) return true ;
41+ return false ;
42+ }
3143
32- protected getDotCompletions ( ) :CompletionItem [ ] {
3344
34- return [
35- CompletionItem . create ( "age" )
45+ DOT_COMPLETIONS :{ [ key :string ] :CompletionItem [ ] } = {
46+ tx :[
47+ {
48+ label :"time" ,
49+ kind :CompletionItemKind . Variable
50+ } ,
51+ {
52+ label :"age" ,
53+ kind :CompletionItemKind . Variable
54+ } ,
55+ {
56+ label :"version" ,
57+ kind :CompletionItemKind . Variable
58+ } ,
59+ {
60+ label :"hashPrevouts" ,
61+ kind :CompletionItemKind . Variable
62+ } ,
63+ {
64+ label :"hashSequence" ,
65+ kind :CompletionItemKind . Variable
66+ } ,
67+ {
68+ label :"outpoint" ,
69+ kind :CompletionItemKind . Variable
70+ } ,
71+ {
72+ label :"bytecode" ,
73+ kind :CompletionItemKind . Variable
74+ } ,
75+ {
76+ label :"value" ,
77+ kind :CompletionItemKind . Variable
78+ } ,
79+ {
80+ label :"sequence" ,
81+ kind :CompletionItemKind . Variable
82+ } ,
83+ {
84+ label :"hashOutputs" ,
85+ kind :CompletionItemKind . Variable
86+ } ,
87+ {
88+ label :"locktime" ,
89+ kind :CompletionItemKind . Variable
90+ } ,
91+ {
92+ label :"hashtype" ,
93+ kind :CompletionItemKind . Variable
94+ } ,
3695 ]
37- const re = / t x \. / ;
38- console . log ( this . lines [ this . pos . line ] ) ;
39- const offset :number = this . doc ?. offsetAt ( this . pos ) || 0 ;
40- const t = this . getCharRange ( offset - 3 , offset )
41- console . log ( "dot: " , t ) ;
42- if ( t === "tx." ) {
43- return [
44- CompletionItem . create ( "age" )
45- ]
96+ }
97+
98+ protected getDotCompletions ( ) :CompletionItem [ ] {
99+
100+ const re = / ( \w + ) .$ / // EX: "tx."
101+ const range :Range = {
102+ start :{ character :0 , line :this . pos . line } ,
103+ end :this . pos
46104 }
47- //console.log(this.doc?.getText().charAt(this.doc?.offsetAt(this.pos)-1));
48- return [
49- CompletionItem . create ( "tx" )
50- ]
105+ const text = this . doc ?. getText ( range ) ;
106+ var arr , keyword ;
107+ if ( ( arr = text ?. match ( re ) ) ) {
108+ keyword = arr [ 1 ] ;
109+ console . log ( "keyword: " , keyword ) ;
110+
111+ return this . DOT_COMPLETIONS [ keyword ] ;
112+ }
113+
114+
115+ return [ ]
51116 }
52117
53118 protected getControlCompletions ( ) :CompletionItem [ ] {
@@ -71,7 +136,7 @@ export default class CompletionService {
71136 {
72137 label :"abs" ,
73138 detail : 'int abs(int a): Returns the absolute value of argument a.' ,
74- insertText : 'abs(${1:value}); ' ,
139+ insertText : 'abs(${1:value})' ,
75140 insertTextFormat : 2 ,
76141 } ,
77142 {
0 commit comments