zed-hexpeek-language-server 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +22 -0
- package/src/server.js +157 -0
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zed-hexpeek-language-server",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A language server used for zed-hexpeek extension, to provide Hover functionality",
|
|
5
|
+
"homepage": "https://github.com/A-23187/zed-hexpeek#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/A-23187/zed-hexpeek/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/A-23187/zed-hexpeek.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "A23187 <a23187.1979421048@gmail.com>",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "server.js",
|
|
17
|
+
"scripts": {},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"vscode-languageserver": "^9.0.1",
|
|
20
|
+
"vscode-languageserver-textdocument": "^1.0.12"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ProposedFeatures,
|
|
3
|
+
TextDocuments,
|
|
4
|
+
createConnection,
|
|
5
|
+
} from "vscode-languageserver/node.js";
|
|
6
|
+
import { TextDocument } from "vscode-languageserver-textdocument";
|
|
7
|
+
|
|
8
|
+
const connection = createConnection(ProposedFeatures.all);
|
|
9
|
+
const documents = new TextDocuments(TextDocument);
|
|
10
|
+
|
|
11
|
+
function fromCharCode(code) {
|
|
12
|
+
if (0 <= code && code <= 31) {
|
|
13
|
+
return String.fromCharCode(code + 0x2400);
|
|
14
|
+
} else if ((32 <= code && code <= 126) || (128 <= code && code <= 255)) {
|
|
15
|
+
return String.fromCharCode(code);
|
|
16
|
+
} else if (code == 127) {
|
|
17
|
+
return "\u2421";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
connection.onInitialize((params) => {
|
|
22
|
+
return { capabilities: { hoverProvider: true } };
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
connection.onHover((params) => {
|
|
26
|
+
const doc = documents.get(params.textDocument.uri);
|
|
27
|
+
if (!doc) {
|
|
28
|
+
connection.console.error(`Document ${params.textDocument.uri} not found`);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const line = doc.getText({
|
|
32
|
+
start: { line: params.position.line, character: 0 },
|
|
33
|
+
end: { line: params.position.line, character: Number.MAX_SAFE_INTEGER },
|
|
34
|
+
});
|
|
35
|
+
let wordStart = params.position.character;
|
|
36
|
+
while (wordStart >= 0 && line.charAt(wordStart).match(/['\w]/)) {
|
|
37
|
+
wordStart--;
|
|
38
|
+
}
|
|
39
|
+
wordStart++;
|
|
40
|
+
let wordEnd = params.position.character;
|
|
41
|
+
while (wordEnd < line.length && line.charAt(wordEnd).match(/['\w]/)) {
|
|
42
|
+
wordEnd++;
|
|
43
|
+
}
|
|
44
|
+
connection.console.log(`wordStart: ${wordStart}, wordEnd: ${wordEnd}`);
|
|
45
|
+
if (wordStart >= wordEnd) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
let word = line.substring(wordStart, wordEnd);
|
|
49
|
+
connection.console.log(`word: ${word}`);
|
|
50
|
+
if (word.charAt(0) == "-") {
|
|
51
|
+
word = word.substring(1);
|
|
52
|
+
wordStart++;
|
|
53
|
+
}
|
|
54
|
+
let num = 0;
|
|
55
|
+
let hexs = "";
|
|
56
|
+
if (word.match(/^(0[bB]['01]*[01]|0[bB][_01]*[01])$/)) {
|
|
57
|
+
connection.console.log("bin");
|
|
58
|
+
for (let i = 0; i < word.length; i++) {
|
|
59
|
+
let ch = word.charAt(i);
|
|
60
|
+
if (ch == "b" || ch == "B" || ch == "'" || ch == "_") {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
num = num * 2 + (ch - "0");
|
|
64
|
+
}
|
|
65
|
+
} else if (word.match(/^(0[oO]?['0-7]*[0-7]|0[oO]?[_0-7]*[0-7])$/)) {
|
|
66
|
+
connection.console.log("oct");
|
|
67
|
+
for (let i = 0; i < word.length; i++) {
|
|
68
|
+
let ch = word.charAt(i);
|
|
69
|
+
if (ch == "o" || ch == "O" || ch == "'" || ch == "_") {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
num = num * 8 + (ch - "0");
|
|
73
|
+
}
|
|
74
|
+
} else if (
|
|
75
|
+
word.match(
|
|
76
|
+
/^(0[xX]['0-9a-fA-F]*[0-9a-fA-F]|0[xX][_0-9a-fA-F]*[0-9a-fA-F])$/,
|
|
77
|
+
)
|
|
78
|
+
) {
|
|
79
|
+
connection.console.log("hex");
|
|
80
|
+
for (let i = 0; i < word.length; i++) {
|
|
81
|
+
let ch = word.charAt(i);
|
|
82
|
+
if (ch == "x" || ch == "X" || ch == "'" || ch == "_") {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
num =
|
|
86
|
+
num * 16 +
|
|
87
|
+
(ch >= "0" && ch <= "9"
|
|
88
|
+
? ch - "0"
|
|
89
|
+
: ch >= "a" && ch <= "f"
|
|
90
|
+
? ch.charCodeAt(0) - 87
|
|
91
|
+
: ch.charCodeAt(0) - 55);
|
|
92
|
+
}
|
|
93
|
+
hexs = word.substring(2);
|
|
94
|
+
} else if (word.match(/^([0-9]|[1-9]['0-9]*[0-9]|[1-9][_0-9]*[0-9])$/)) {
|
|
95
|
+
connection.console.log("dec");
|
|
96
|
+
for (let i = 0; i < word.length; i++) {
|
|
97
|
+
let ch = word.charAt(i);
|
|
98
|
+
if (ch == "'" || ch == "_") {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
num = num * 10 + (ch - "0");
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
hexs = hexs || num.toString(16);
|
|
107
|
+
connection.console.log(`hex: ${hexs}`);
|
|
108
|
+
let numInLE = 0;
|
|
109
|
+
let ascii = "";
|
|
110
|
+
for (let i = hexs.length - 2; i >= 0; i -= 2) {
|
|
111
|
+
numInLE = numInLE * 256 + parseInt(hexs.substring(i, i + 2), 16);
|
|
112
|
+
}
|
|
113
|
+
if (hexs.length % 2 == 1) {
|
|
114
|
+
const v = parseInt(hexs.substring(0, 1), 16);
|
|
115
|
+
numInLE = numInLE * 256 + v;
|
|
116
|
+
ascii = fromCharCode(v);
|
|
117
|
+
}
|
|
118
|
+
for (let i = hexs.length % 2; i < hexs.length; i += 2) {
|
|
119
|
+
const v = parseInt(hexs.substring(i, i + 2), 16);
|
|
120
|
+
connection.console.info(`v: ${v}`);
|
|
121
|
+
ascii += fromCharCode(v);
|
|
122
|
+
}
|
|
123
|
+
connection.console.log(`numInLE: ${num}`);
|
|
124
|
+
connection.console.log(`ascii: ${ascii}`);
|
|
125
|
+
return {
|
|
126
|
+
contents: {
|
|
127
|
+
kind: "markdown",
|
|
128
|
+
value: `[**HexPeek**](https://github.com/A-23187/zed-hexpeek) \`${word}\`
|
|
129
|
+
\`\`\`
|
|
130
|
+
Binary: 0b${num.toString(2)}
|
|
131
|
+
Octal: 0o${num.toString(8)}
|
|
132
|
+
Decimal:
|
|
133
|
+
in BE: ${num}
|
|
134
|
+
in LE: ${numInLE}
|
|
135
|
+
Hexadecimal: 0x${hexs}
|
|
136
|
+
Ascii: ${ascii}
|
|
137
|
+
Time:
|
|
138
|
+
in S: ${new Date(num * 1000).toISOString()}
|
|
139
|
+
in MS: ${new Date(num).toISOString()}
|
|
140
|
+
\`\`\`
|
|
141
|
+
`,
|
|
142
|
+
},
|
|
143
|
+
range: {
|
|
144
|
+
start: {
|
|
145
|
+
line: params.position.line,
|
|
146
|
+
character: wordStart,
|
|
147
|
+
},
|
|
148
|
+
end: {
|
|
149
|
+
line: params.position.line,
|
|
150
|
+
character: wordEnd,
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
documents.listen(connection);
|
|
157
|
+
connection.listen();
|