-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
141 lines (125 loc) · 5.67 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
let builtInItems = [];
/**
* Activate the extension
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const globalsPath = path.join(__dirname, 'globals');
const constantsPath = path.join(__dirname, 'constants');
const functionsPath = path.join(__dirname, 'functions');
const propertiesPath = path.join(__dirname, 'reserved_properties');
const sunvoxPath = path.join(__dirname, 'sunvox');
function loadItemsFromDirectory(directory, type) {
if (!fs.existsSync(directory)) {
console.error(`Directory does not exist: ${directory}`);
return [];
}
let items = [];
fs.readdirSync(directory).forEach(file => {
const filePath = path.join(directory, file);
if (fs.statSync(filePath).isDirectory()) {
// If it's a directory, recursively load items
items = items.concat(loadItemsFromDirectory(filePath, type));
} else if (file.endsWith('.js')) {
// If it's a file, require it and load its items
const itemsFromFile = require(filePath);
itemsFromFile.forEach(item => {
item.type = type;
items.push(item);
});
}
});
return items;
}
const globals = loadItemsFromDirectory(globalsPath, 'global');
const constants = loadItemsFromDirectory(constantsPath, 'constant');
const functions = loadItemsFromDirectory(functionsPath, 'function');
const properties = loadItemsFromDirectory(propertiesPath, 'property');
const sunvoxConstants = loadItemsFromDirectory(path.join(sunvoxPath, 'constants'), 'sunvox-constant');
const sunvoxFunctions = loadItemsFromDirectory(path.join(sunvoxPath, 'functions'), 'sunvox-function');
builtInItems = [...globals, ...constants, ...functions, ...properties, ...sunvoxConstants, ...sunvoxFunctions];
// Register CompletionItemProvider for Pixilang
const provider = vscode.languages.registerCompletionItemProvider(
{ language: 'pixilang', scheme: 'file' },
{
provideCompletionItems(document, position, token, context) {
return builtInItems.map(item => {
let labelPrefix;
let completionItemKind;
switch (item.type) {
case 'global':
labelPrefix = 'global';
completionItemKind = vscode.CompletionItemKind.Variable;
break;
case 'constant':
labelPrefix = 'cons';
completionItemKind = vscode.CompletionItemKind.Constant;
break;
case 'function':
labelPrefix = 'fn';
completionItemKind = vscode.CompletionItemKind.Function;
break;
case 'property':
labelPrefix = 'prop';
completionItemKind = vscode.CompletionItemKind.Property;
break;
case 'sunvox-constant':
labelPrefix = 'sv-con';
completionItemKind = vscode.CompletionItemKind.Constant;
break;
case 'sunvox-function':
labelPrefix = 'sv-fn';
completionItemKind = vscode.CompletionItemKind.Function;
break;
default:
labelPrefix = '';
completionItemKind = vscode.CompletionItemKind.Text;
}
const completionItem = new vscode.CompletionItem(
item.name,
completionItemKind
);
completionItem.label = { label: `${labelPrefix} ${item.name}`, description: '' };
completionItem.insertText = item.name;
completionItem.detail = item.detail;
completionItem.documentation = new vscode.MarkdownString(item.documentation);
return completionItem;
});
},
},
'(', // Trigger IntelliSense for functions on open parenthesis
'.', // Trigger IntelliSense for constants/properties/globals
' ', // Trigger IntelliSense after a space
'=' // Trigger IntelliSense after an assignment
);
const hoverProvider = vscode.languages.registerHoverProvider(
{ language: 'pixilang', scheme: 'file' },
{
provideHover(document, position) {
const wordRange = document.getWordRangeAtPosition(position);
const word = document.getText(wordRange);
const item = builtInItems.find((item) => item.name === word);
if (item) {
const markdown = new vscode.MarkdownString();
markdown.appendCodeblock(`${item.name}(${item.parameters || ''})`, 'pixilang');
markdown.appendMarkdown(`\n\n${item.documentation || 'No documentation available.'}`);
return new vscode.Hover(markdown);
}
return null;
},
}
);
context.subscriptions.push(provider);
context.subscriptions.push(hoverProvider);
}
/**
* Deactivate the extension
*/
function deactivate() {}
module.exports = {
activate,
deactivate,
};