Skip to content

Commit 4f64831

Browse files
dot completion
1 parent 49647b3 commit 4f64831

4 files changed

Lines changed: 93 additions & 29 deletions

File tree

client/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function activate(context: ExtensionContext) {
122122
vscode.languages.registerHoverProvider('cashscript', new CashscriptHoverProvider(outputChannel));
123123
vscode.languages.registerSignatureHelpProvider('cashscript', new CashscriptSignatureCompleter(outputChannel), '(');
124124

125+
125126
// Start the client. This will also launch the server
126127
client.start();
127128
}

client/testFixture/test.cash

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
1717
; require(checkSig(recipientSig, recipient));
1818
}
1919

20-
2120

2221
// Require timeout time to be reached and sender's signature to match
2322
function timeout(sig senderSig) {

server/src/completionService.ts

Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,118 @@
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

44
export 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 = /tx\./;
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
{

server/src/server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ connection.onInitialize((params: InitializeParams) => {
5353
textDocumentSync: TextDocumentSyncKind.Incremental,
5454
// Tell the client that this server supports code completion.
5555
completionProvider: {
56-
resolveProvider: true
56+
resolveProvider: true,
57+
triggerCharacters:[".",]
5758
},
5859
}
5960
};
@@ -153,7 +154,6 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
153154
// In this simple example we get the settings for every validate run.
154155
let settings = await getDocumentSettings(textDocument.uri);
155156

156-
console.log("triggered bo");
157157
isValidatingFile = false;
158158

159159
// The validator creates diagnostics for all uppercase words length 2 and more
@@ -216,7 +216,6 @@ connection.onCompletion(
216216
// The pass parameter contains the position of the text document in
217217
// which code complete got requested. For the example we ignore this
218218
// info and always provide the same completion items.
219-
console.log("completion")
220219
const doc: TextDocument|undefined = documents.get(_textDocumentPosition.textDocument.uri);
221220
const pos: Position = _textDocumentPosition.position;
222221
const cs = new CompletionService(doc, pos);

0 commit comments

Comments
 (0)