-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
179 lines (160 loc) · 4.38 KB
/
extension.js
File metadata and controls
179 lines (160 loc) · 4.38 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
const vscode = require('vscode');
const { execSync } = require('child_process');
const {
LanguageClient,
LanguageClientOptions,
ServerOptions,
} = require('vscode-languageclient/node');
let client;
/**
* Activate the Duso language extension with LSP support
*/
async function activate(context) {
console.log('Duso extension activated');
// Server options: launch duso LSP server
const serverOptions = {
command: 'duso',
args: ['-lsp'],
};
// Client options
const clientOptions = {
documentSelector: [{ scheme: 'file', language: 'duso' }],
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher('**/.du'),
},
};
// Create and start the language client
client = new LanguageClient(
'duso',
'Duso Language Server',
serverOptions,
clientOptions
);
try {
await client.start();
console.log('Duso language server started');
} catch (err) {
vscode.window.showErrorMessage(
`Failed to start Duso language server: ${err.message}`
);
console.error('Failed to start Duso LSP:', err);
}
// Register command to view full documentation
const viewDocCommand = vscode.commands.registerCommand(
'duso.viewReference',
async (name) => {
try {
// Run duso -no-color -doc <name>
const output = execSync(`duso -no-color -doc ${name}`, {
encoding: 'utf-8',
});
// Create and show a webview panel
const panel = vscode.window.createWebviewPanel(
'dusoDoc',
`Duso: ${name}`,
vscode.ViewColumn.Beside,
{}
);
// Convert markdown to HTML (simple conversion)
const html = markdownToHtml(output);
panel.webview.html = html;
} catch (err) {
vscode.window.showErrorMessage(
`Failed to get documentation for ${name}: ${err.message}`
);
}
}
);
// Register command to run/build current Duso script
const runScriptCommand = vscode.commands.registerCommand(
'duso.runScript',
async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No file open');
return;
}
const fileName = editor.document.fileName;
// Create or reuse integrated terminal
let terminal = vscode.window.terminals.find(t => t.name === 'Duso');
if (!terminal) {
terminal = vscode.window.createTerminal('Duso');
}
terminal.show();
// Run the script
terminal.sendText(`duso "${fileName}"`, true);
}
);
context.subscriptions.push(client);
context.subscriptions.push(viewDocCommand);
context.subscriptions.push(runScriptCommand);
}
/**
* Simple markdown to HTML converter for documentation display
*/
function markdownToHtml(markdown) {
let html = markdown
// Headers
.replace(/^### (.*?)$/gm, '<h3>$1</h3>')
.replace(/^## (.*?)$/gm, '<h2>$1</h2>')
.replace(/^# (.*?)$/gm, '<h1>$1</h1>')
// Code blocks
.replace(/```[\s\S]*?```/gm, (match) => {
const code = match.replace(/```/g, '').trim();
return `<pre><code>${escapeHtml(code)}</code></pre>`;
})
// Inline code
.replace(/`([^`]+)`/g, '<code>$1</code>')
// Bold
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
// Italic
.replace(/\*(.*?)\*/g, '<em>$1</em>')
// Lists
.replace(/^- (.*?)$/gm, '<li>$1</li>')
// Line breaks
.replace(/\n\n/g, '</p><p>')
.replace(/\n/g, '<br>');
return `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; padding: 20px; }
h1, h2, h3 { color: #333; }
code { background: #f5f5f5; padding: 2px 6px; border-radius: 3px; font-family: 'Courier New', monospace; }
pre { background: #f5f5f5; padding: 12px; border-radius: 5px; overflow-x: auto; }
li { margin: 5px 0; }
</style>
</head>
<body>
<p>${html}</p>
</body>
</html>
`;
}
/**
* Escape HTML special characters
*/
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
return text.replace(/[&<>"']/g, (m) => map[m]);
}
/**
* Deactivate the extension
*/
function deactivate() {
if (!client) {
return undefined;
}
return client.stop();
}
module.exports = {
activate,
deactivate,
};