-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
207 lines (166 loc) · 5.32 KB
/
Copy pathextension.js
File metadata and controls
207 lines (166 loc) · 5.32 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
require('./src/cfg');
const path = require('path');
const fs = require('fs');
const mirror = require('./src/mirror');
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function msg(string) {
vscode.window.showInformationMessage("SourceMirror: " + string)
};
function msgWarn(string) {
vscode.window.showWarningMessage("SourceMirror: " + string)
};
function msgError(string) {
vscode.window.showErrorMessage("SourceMirror: " + string)
};
function openOrCreateSourceMirrorFile(shouldOpen) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
const rootPath = workspaceFolders[0].uri.fsPath;
const sourceMirrorFilePath = path.join(rootPath, '.sourcemirror');
fs.access(sourceMirrorFilePath, fs.constants.F_OK, (err) => {
if (err) {
// The file doesn't exist, create it if shouldOpen is false
if (!shouldOpen) {
fs.writeFile(sourceMirrorFilePath, '', (err) => {
if (err) {
console.error('Failed to create .sourcemirror file:', err);
return;
}
console.log('.sourcemirror file created successfully.');
});
}
} else if (shouldOpen) {
// The file exists and should be opened
openTextDocument(sourceMirrorFilePath);
}
if (!shouldOpen) {
// Read the file content
/*
fs.readFile(sourceMirrorFilePath, 'utf8', (err, data) => {
if (err) {
console.error('Failed to read .sourcemirror file:', err);
return;
}
console.log('.sourcemirror file content:', data);
});
*/
return true;
}
});
} else {
vscode.window.showInformationMessage('No workspace is currently open.');
}
}
function openTextDocument(filePath) {
vscode.workspace.openTextDocument(filePath).then((document) => {
vscode.window.showTextDocument(document);
}).then(() => {
console.log('.sourcemirror file opened successfully.');
}, (error) => {
console.error('Failed to open .sourcemirror file:', error);
});
}
function start(autoLoaded) {
openOrCreateSourceMirrorFile();
const workspaceFolders = vscode.workspace.workspaceFolders;
let sourceMirrorFilePath = "";
let rootPath;
if (workspaceFolders && workspaceFolders.length > 0) {
rootPath = workspaceFolders[0].uri.fsPath;
sourceMirrorFilePath = path.join(rootPath, '.sourcemirror');
}
mirror.startMirror(sourceMirrorFilePath, rootPath, 1000);
global.extension.Running = true;
if (autoLoaded) {
msg("Autostarted");
} else {
msg("Running!")
};
};
function stop(){
mirror.stopMirror();
global.extension.Running = false;
msg("Stopped!")
}
function configure(){
openOrCreateSourceMirrorFile(true);
}
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "sourcemirror" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
/*
let disposable = vscode.commands.registerCommand('sourcemirror.helloWorld', function () {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World from SourceMirror!' + mirror.testMessage());
//vscode.window.showInformationMessage('Hello World from SourceMirror!');
});
*/
const disposables = [
vscode.commands.registerCommand('sourcemirror.start', function () {
//vscode.window.showInformationMessage('Attempting to start');
if (global.extension.Running) {
msgWarn("Already running!");
} else {
msg("Starting...");
start();
}
}),
vscode.commands.registerCommand('sourcemirror.stop', function () {
if (!global.extension.Running) {
msgWarn("Not running!");
} else {
msg("Stopping...");
stop();
}
}),
vscode.commands.registerCommand('sourcemirror.configure', function () {
msg("Opening configuration");
configure();
}),
vscode.commands.registerCommand('sourcemirror.reload', function () {
msg("Reloading...");
if (!global.extension.Running) { stop(); };
start();
}),
];
disposables.forEach(disposable => context.subscriptions.push(disposable));
//context.subscriptions.push(disposable);
const configuration = vscode.workspace.getConfiguration('sourcemirror');
if (configuration.get('autoStart')) {
start(true);
}
}
// This method is called when your extension is deactivated
function deactivate() {
if (global.extension.Running) {
stop();
}
}
module.exports = {
activate,
deactivate
}
/**
const vscode = require('vscode');
function getExtensionSettings() {
const configuration = vscode.workspace.getConfiguration('myExtension');
const enableFeature = configuration.get('enableFeature');
const username = configuration.get('username');
console.log(`Enable Feature: ${enableFeature}`);
console.log(`Username: ${username}`);
}
// Example usage
getExtensionSettings();
*/