This repository was archived by the owner on Jan 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.js
More file actions
188 lines (184 loc) · 8.49 KB
/
extension.js
File metadata and controls
188 lines (184 loc) · 8.49 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// IMPORTS
const vscode = require('vscode')
const fs = require("fs")
const path = require("path")
// MAIN FUNCTION
function activate(context) {
// CONFIGURATION
const config = vscode.workspace.getConfiguration('csscribe');
const language = config.get('language');
const style = config.get("style");
console.log(`Extension activated with language ${language} and style ${style}.`);
// ENSURE TMRULES ARE APPLIED ON STARTUP
vscode.workspace.onDidOpenTextDocument((document) => {
if (document.languageId === 'cssc') {
console.log("Applying textMateRules for CSScribe.");
vscode.workspace.getConfiguration('editor').update('tokenColorCustomizations', {
textMateRules: vscode.workspace.getConfiguration('csscribe').get('editor.tokenColorCustomizations.textMateRules')
}, vscode.ConfigurationTarget.Global);
}
});
// APPLY TMRULES ON ALL CSSC DOCUMENTS
vscode.workspace.textDocuments.forEach((document) => {
if (document.languageId === 'cssc') {
console.log("Applying textMateRules for already open CSScribe documents.");
vscode.workspace.getConfiguration('editor').update('tokenColorCustomizations', {
textMateRules: vscode.workspace.getConfiguration('csscribe').get('editor.tokenColorCustomizations.textMateRules')
}, vscode.ConfigurationTarget.Global);
}
});
// RUN COMPILER COMMAND
let runCompiler = vscode.commands.registerCommand('csscribe.runCompiler', () => {
console.log("Executed: csscribe.runCompiler");
const compilerPath = context.asAbsolutePath("compiler.exe");
const terminal = vscode.window.createTerminal('Compiler Terminal');
terminal.sendText(`${compilerPath} ${language}`);
terminal.show();
vscode.commands.executeCommand('csscribe.parseStyle');
});
context.subscriptions.push(runCompiler);
// RUN BUILDER COMMAND
let runBuilder = vscode.commands.registerCommand('csscribe.runBuilder', () => {
console.log("Executed: csscribe.runBuilder");
const builderPath = context.asAbsolutePath('builder.exe');
const terminal = vscode.window.createTerminal('Builder Terminal');
terminal.sendText(`${builderPath} ${language}`);
terminal.show();
vscode.commands.executeCommand('csscribe.parseStyle');
});
context.subscriptions.push(runBuilder);
// RUN JPG2PNG COMMAND
let runJPG2PNG = vscode.commands.registerCommand('csscribe.runJPG2PNG', () => {
console.log("Executed: csscribe.runJPG2PNG");
const JPG2PNGpath = context.asAbsolutePath('jpg2png.exe');
const terminal = vscode.window.createTerminal('JPG to PNG Terminal');
terminal.sendText(`${JPG2PNGpath} ${language}`);
terminal.show();
});
context.subscriptions.push(runJPG2PNG);
// RUN VERSIONING SYSTEM
let runVersioningSystem = vscode.commands.registerCommand("csscribe.runVersioningSystem", () => {
console.log("Executed: csscribe.runVersioningSystem");
const versioningSystemPath = context.asAbsolutePath("versioning.exe");
const terminal = vscode.window.createTerminal("Versioning System Terminal");
terminal.sendText(`${versioningSystemPath} ${language}`);
terminal.show();
});
context.subscriptions.push(runVersioningSystem);
// PARSE STYLE
let parseStyle = vscode.commands.registerCommand("csscribe.parseStyle", () => {
console.log("Executed: csscribe.parseStyle");
const stylePath = context.asAbsolutePath(`style/${style}.less`);
const fontsPath = context.asAbsolutePath('fonts');
const targetDir = path.join(vscode.workspace.rootPath, ".crossnote");
const targetPath = path.join(targetDir, "style.less");
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir);
}
if (fs.existsSync(targetPath)) {
return;
}
fs.readFile(stylePath, "utf-8", (err, data) => {
if (err) {
console.log(`Error reading file from disk: ${err}`)
} else {
const updatedData = data.replace(/¿\?/g, fontsPath.replace(/\\/g, '/'));
fs.writeFile(targetPath, updatedData, "utf-8", (err) => {
if (err) {
console.log(`Error writing file to disk: ${err}`);
} else {
console.log(`${style}.less pasted successfully into the workspace.`);
}
});
}
});
});
context.subscriptions.push(parseStyle);
// COMPLETION ITEM PROVIDER
const completionProvider = vscode.languages.registerCompletionItemProvider('cssc', {
provideCompletionItems(document, position, token, completionContext) {
try {
// DEFINE PATHS AND REQUIREMENTS
const completionWordsPath = path.join(context.extensionPath, `language/${language}.json`);
const completionWords = require(completionWordsPath);
// INITIALIZE VARIABLES
const linePrefix = document.lineAt(position).text.substr(0, position.character);
const text = document.getText();
// BUILD WORD FREQUENCY MAP
const wordFrequency = {};
const words = text.match(/\b\w+\b/g);
if (words) {
words.forEach(word => {
const lowerWord = word.toLowerCase();
wordFrequency[lowerWord] = (wordFrequency[lowerWord] || 0) + 1;
});
}
// FUNCTION TO SET sortText BASED ON FREQUENCY
const setSortText = (item, frequency) => {
// Invert frequency for correct sorting (higher frequency = should appear first)
// Pad with zeros to ensure proper lexicographical sorting
const invertedFrequency = String(99999 - frequency).padStart(5, '0');
item.sortText = invertedFrequency + item.label;
};
// FUNCTION TO CAPITALIZE FIRST LETTER
const capitalizeFirstLetter = (word) => {
return word.charAt(0).toUpperCase() + word.slice(1);
};
// DETERMINE IF CURRENT WORD STARTS WITH UPPERCASE
const currentWordRange = document.getWordRangeAtPosition(position);
const currentWord = currentWordRange ? document.getText(currentWordRange) : '';
const isCurrentWordCapitalized = currentWord.charAt(0) === currentWord.charAt(0).toUpperCase();
// CREATE COMPLETION ITEMS FROM completionWords
let completionItems = completionWords.map(word => {
// Skip single-letter words
if (word.length === 1) {
return null;
}
const item = new vscode.CompletionItem(word, vscode.CompletionItemKind.Text);
setSortText(item, wordFrequency[word.toLowerCase()] || 0);
// Capitalize if current word starts with uppercase
if (isCurrentWordCapitalized) {
item.label = capitalizeFirstLetter(item.label);
}
return item;
}).filter(item => item !== null);
// ADD WORDS ALREADY IN THE FILE
const wordsInFile = new Set(words);
wordsInFile.forEach(word => {
// Skip single-letter words
if (word.length === 1) {
return;
}
const item = new vscode.CompletionItem(word, vscode.CompletionItemKind.Text);
setSortText(item, wordFrequency[word.toLowerCase()]);
// Capitalize if current word starts with uppercase
if (isCurrentWordCapitalized) {
item.label = capitalizeFirstLetter(item.label);
}
completionItems.push(item);
});
// REMOVE DUPLICATE ITEMS
const uniqueItems = new Map();
completionItems.forEach(item => {
uniqueItems.set(item.label, item);
});
// RETURN SORTED ITEMS
return Array.from(uniqueItems.values());
} catch (error) {
console.error(`Error loading completion words or commands: ${error}`);
return [];
}
}
});
context.subscriptions.push(completionProvider);
}
// DEACTIVATION FUNCTION
function deactivate() {
// LOG DEACTIVATION
console.log("Extension deactivated.")
}
// EXPORT FUNCTIONS
module.exports = {
activate,
deactivate
};