Skip to content

Commit c44cd96

Browse files
completion + sig
1 parent 4f64831 commit c44cd96

10 files changed

Lines changed: 129 additions & 52 deletions

File tree

client/src/CashscriptHoverProvider.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,39 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
1313
let range = document.getWordRangeAtPosition(position, this.re);
1414
let word = document.getText(range);
1515

16-
17-
this.channel.appendLine("hover: "+word)
18-
return new vscode.Hover(this.getHoverAnnotation(word), range);
16+
this.channel.appendLine("hoverword: "+word)
17+
const annotation = this.getHoverAnnotation(word);
18+
if(annotation) return new vscode.Hover(annotation, range);
19+
20+
21+
// check special words
22+
23+
return new vscode.Hover(this.getMiscellaneousHovers(document, position), range);
1924
}
2025

2126

22-
getHoverAnnotation(word:string){
23-
24-
const data = LANGUAGE[word];
27+
getHoverAnnotation(word:string):vscode.MarkdownString[]{
28+
29+
const data = LANGUAGE[word] || null;
30+
if(!data) return null;
31+
2532
return [
2633
new vscode.MarkdownString().appendCodeblock(data.code),
2734
new vscode.MarkdownString(data.codeDesc)
2835
]
29-
36+
}
37+
38+
39+
getMiscellaneousHovers(document:vscode.TextDocument, position:vscode.Position):vscode.MarkdownString[]{
40+
41+
const reg = /(contract|function) (\w+)/;
42+
let range = document.getWordRangeAtPosition(position, reg);
43+
let word = document.getText(range);
44+
if(word.includes("\n")) return null;
45+
46+
return [
47+
new vscode.MarkdownString().appendCodeblock(word)
48+
];
3049
}
3150

3251

client/src/LanguageDesc.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ let OUTPUT_INSTANTIATION:Data = {
7575
},
7676
}
7777

78+
let STATEMENTS:Data = {
79+
require:{
80+
code:'null require( exp )',
81+
codeDesc:"Takes a boolean expression, if it evaluates to 'false' the contract fails. Used to ensure requirements"
82+
}
83+
}
7884

7985

80-
let LANGUAGE:Data = {...GLOBAL_FUNCTIONS, ...OUTPUT_INSTANTIATION};
86+
let LANGUAGE:Data = {...GLOBAL_FUNCTIONS, ...OUTPUT_INSTANTIATION, ...STATEMENTS};
8187

8288
export { GLOBAL_FUNCTIONS, OUTPUT_INSTANTIATION, LANGUAGE };

client/testFixture/P2PKH.cash

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ contract P2PKH(bytes20 pkh) {
55
require(hash160(pk) == pkh);
66
require(checkSig(s, pk));
77
require(checkSig(s, pk));
8-
require(checkSig(s, pk));
9-
require(checkSig(s, pk));
10-
require(checkSig(s, pk));
11-
require(checkSig(s, pk));
12-
require(checkSig(s, pk));
13-
require(checkSig(s, pk));
14-
require(checkSig(s, pk));
15-
require(checkSig(s, pk));
16-
require(checkSig(s, pk));
17-
require(checkSig(s, pk));
18-
require(checkSig(s, pk));
19-
require(checkSig(s, pk));
20-
require(checkSig(s, pk));
21-
require(checkSig(s, pk));
22-
require(checkSig(s, pk));
23-
require(checkSig(s, pk));
24-
require(checkSig(s, pk));
25-
require(checkSig(s, pk));
268
}
279

2810
}

client/testFixture/P2PKH.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
"name": "cashc",
2929
"version": "0.6.0"
3030
},
31-
"updatedAt": "2021-04-26T15:20:05.568Z"
31+
"updatedAt": "2021-04-27T15:13:57.815Z"
3232
}

client/testFixture/p.cash

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pragma cashscript ^0.5.6;
2+
3+
contract MyContract() {
4+
5+
function myFunction(int num) {
6+
7+
}
8+
9+
}

client/testFixture/t.cash

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ pragma cashscript ^0.6.0;
33
contract ContractName() {
44
function test() {
55
require(true);
6-
7-
}
8-
9-
function nameTest(int name) {
10-
require(name == 2);
6+
string x = "asdasd";
117
}
128
}

client/testFixture/t.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@
66
"name": "test",
77
"covenant": false,
88
"inputs": []
9-
},
10-
{
11-
"name": "nameTest",
12-
"covenant": false,
13-
"inputs": [
14-
{
15-
"name": "name",
16-
"type": "int"
17-
}
18-
]
199
}
2010
],
21-
"bytecode": "OP_DUP OP_0 OP_NUMEQUAL OP_IF OP_DROP OP_1 OP_ELSE OP_1 OP_NUMEQUALVERIFY OP_2 OP_NUMEQUAL OP_ENDIF",
22-
"source": "pragma cashscript ^0.6.0;\n\ncontract ContractName() {\n function test() {\n require(true);\n }\n\n function nameTest(int name) {\n require(name == 2);\n }\n}\n",
11+
"bytecode": "OP_1",
12+
"source": "pragma cashscript ^0.6.0;\r\n\r\ncontract ContractName() {\r\n function test() {\r\n require(true);\r\n }\r\n}\r\n",
2313
"compiler": {
2414
"name": "cashc",
2515
"version": "0.6.0"
2616
},
27-
"updatedAt": "2021-03-21T20:55:41.683Z"
17+
"updatedAt": "2021-04-27T15:14:39.459Z"
2818
}

server/src/completionService.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class CompletionService {
2121
return this.getDotCompletions();
2222
}
2323

24+
completions = completions.concat(this.getConditionalCompletions());
2425
completions = completions.concat(this.getControlCompletions());
2526
completions = completions.concat(this.getGlobalFunctionCompletions());
2627
completions = completions.concat(this.getOutputCompletions());
@@ -114,6 +115,20 @@ export default class CompletionService {
114115

115116
return []
116117
}
118+
119+
protected getConditionalCompletions():CompletionItem[]{
120+
const completions:CompletionItem[] = [];
121+
if(!this.text.includes("contract")){
122+
completions.push({
123+
label:"contract",
124+
detail:"Instantiate a new Contract",
125+
insertText:"contract ${1:ContractName}($2) {\n\n}",
126+
insertTextFormat:2
127+
});
128+
}
129+
130+
return completions;
131+
}
117132

118133
protected getControlCompletions():CompletionItem[]{
119134
const words = ["pragma", "cashscript", "if", "else", "require"]
@@ -132,6 +147,7 @@ export default class CompletionService {
132147
"ripemd160", "sha1", "sha256", "hash160","hash256",
133148
"checkSig", "checkMultiSig", "checkDataSig"]
134149

150+
/*
135151
const comps: CompletionItem[] = [
136152
{
137153
label:"abs",
@@ -193,6 +209,69 @@ export default class CompletionService {
193209
insertText:"checkDataSig(${1:signature}, ${2:message}, ${3:pubkey})",
194210
insertTextFormat:2
195211
}
212+
]*/
213+
214+
const comps: CompletionItem[] = [
215+
{
216+
label:"abs",
217+
detail: 'int abs(int a): Returns the absolute value of argument a.',
218+
insertText: 'abs',
219+
insertTextFormat: 2,
220+
},
221+
{
222+
label:"min",
223+
detail:"int min(int a, int b): Returns the minimum value of arguments `a` and `b`.",
224+
insertText:"min",
225+
insertTextFormat:2
226+
},
227+
{
228+
label:"max",
229+
detail:"int max(int a, int b): Returns the maximum value of arguments `a` and `b`.",
230+
insertText:"max",
231+
insertTextFormat:2
232+
},
233+
{
234+
label:"within",
235+
detail:"bool within(int x, int lower, int upper): Returns `true` if and only if `x >= lower && x < upper`.",
236+
insertText:"within",
237+
insertTextFormat:2
238+
},
239+
{
240+
label:"ripemd160",
241+
detail:"bytes20 ripemd160(any x): Returns the SHA-1 hash of argument `x`.",
242+
insertText:"ripemd160",
243+
insertTextFormat:2
244+
},
245+
{
246+
label:"sha256",
247+
detail:"bytes32 sha256(any x): Returns the SHA-256 hash of argument `x`.",
248+
insertText:"sha256",
249+
insertTextFormat:2
250+
},
251+
{
252+
label:"hash160",
253+
detail:"bytes20 hash160(any x): Returns the RIPEMD-160 hash of the SHA-256 hash of argument `x`.",
254+
insertText:"hash160",
255+
insertTextFormat:2
256+
},
257+
{
258+
label:"hash256",
259+
detail:"bytes32 hash256(any x): bytes32 hash256(any x)",
260+
insertText:"hash256",
261+
insertTextFormat:2
262+
},
263+
{
264+
label:"checkMultiSig",
265+
detail:"bool checkMultiSig(sig[] sigs, pubkey[] pks): Performs a multi-signature check using a list of transaction signatures and public keys.",
266+
insertText:"checkMultiSig",
267+
insertTextFormat:2
268+
},
269+
{
270+
label:"checkDataSig",
271+
detail:"bool checkDataSig(datasig s, bytes msg, pubkey pk): Checks that sig `s` is a valid signature for message `msg` and matches with public key `pk`.",
272+
insertText:"checkDataSig",
273+
insertTextFormat:2
274+
}
196275
]
197276

198277
return comps;

server/src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ connection.onCompletion(
224224
);
225225

226226

227+
227228
// connection.onHover(
228229
// () => {
229230

snippets/cashscript.snippets.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
"description":"Set semantic version"
66
},
77

8-
"Contract instantiation" : {
9-
"prefix": "contract",
10-
"body":["contract ${1:ContractName}($2) {\n\n}"],
11-
"description":"Instantiate Contract"
12-
},
138

14-
"Create Function" : {
9+
"Function Statement" : {
1510
"prefix":"function",
16-
"body":"function ${1:functionName}($2) {\n\t$3\n}",
11+
"body":"function ${1:functionName}(${2:params}) {\n\t$3\n}",
1712
"description":"Create function"
1813
},
1914

0 commit comments

Comments
 (0)