first
This commit is contained in:
91
src/extension.ts
Normal file
91
src/extension.ts
Normal file
@ -0,0 +1,91 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
console.log('Congratulations, your extension "vscodestat" is now active!');
|
||||
|
||||
// crée une commande de test
|
||||
let disposable = vscode.commands.registerCommand('vscodestat.helloWorld', () => {
|
||||
vscode.window.showInformationMessage('Hello World from vscodestat!');
|
||||
});
|
||||
context.subscriptions.push(disposable);
|
||||
|
||||
// commande pour definir l'url
|
||||
let disposable2 = vscode.commands.registerCommand('vscodestat.setUrl', async () => {
|
||||
const url = await vscode.window.showInputBox({
|
||||
prompt: 'Enter the URL:',
|
||||
placeHolder: 'https://example.com'
|
||||
});
|
||||
|
||||
if (url) {
|
||||
vscode.workspace.getConfiguration().update('vscodestat.url', url, vscode.ConfigurationTarget.Global);
|
||||
vscode.window.showInformationMessage('URL set successfully.');
|
||||
}
|
||||
});
|
||||
context.subscriptions.push(disposable2);
|
||||
|
||||
// commande pour tester l'url
|
||||
let disposable3 = vscode.commands.registerCommand('vscodestat.callUrl', async () => {
|
||||
const storedUrl = vscode.workspace.getConfiguration().get('vscodestat.url') as string;
|
||||
if (!storedUrl) {
|
||||
vscode.window.showWarningMessage('URL is not set. Please set the URL first.');
|
||||
return;
|
||||
}
|
||||
await makeHttpRequest({});
|
||||
});
|
||||
context.subscriptions.push(disposable3);
|
||||
|
||||
// fonction qui envoie un event au serveur
|
||||
async function makeHttpRequest(json: Object) {
|
||||
try {
|
||||
const url = vscode.workspace.getConfiguration().get('vscodestat.url') as string;
|
||||
if (!url) {
|
||||
return;
|
||||
}
|
||||
|
||||
vscode.window.showInformationMessage('Calling URL: ' + url);
|
||||
const rawResponse = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(json)
|
||||
});
|
||||
const content = await rawResponse.json();
|
||||
|
||||
console.log(content);
|
||||
vscode.window.showInformationMessage('HTTP request successful.');
|
||||
} catch (error:any) {
|
||||
console.error('Error making HTTP request:', error);
|
||||
vscode.window.showErrorMessage('Error making HTTP request: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// detecte une ouverture de fichier / nouvel onglet
|
||||
vscode.window.onDidChangeActiveTextEditor(async editor => {
|
||||
if (editor) {
|
||||
const filePath = editor.document.fileName;
|
||||
console.log('Opened file:', filePath);
|
||||
|
||||
vscode.window.showInformationMessage(`Opened file: ${filePath}`);
|
||||
await makeHttpRequest({ opened: filePath });
|
||||
}
|
||||
});
|
||||
|
||||
// detecte une sauvegarde de fichier
|
||||
vscode.workspace.onDidSaveTextDocument(async document => {
|
||||
const filePath = document.fileName;
|
||||
console.log('Saved file:', filePath);
|
||||
|
||||
vscode.window.showInformationMessage(`Saved file: ${filePath}`);
|
||||
await makeHttpRequest({ saved: filePath });
|
||||
});
|
||||
|
||||
// detecte un focus / blur de la fenetre vscode
|
||||
vscode.window.onDidChangeWindowState(async event => {
|
||||
console.log('Window state changed:', event.focused);
|
||||
await makeHttpRequest({ focus: event.focused });
|
||||
});
|
||||
}
|
||||
|
||||
export function deactivate() { }
|
15
src/test/extension.test.ts
Normal file
15
src/test/extension.test.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import * as assert from 'assert';
|
||||
|
||||
// You can import and use all API from the 'vscode' module
|
||||
// as well as import your extension to test it
|
||||
import * as vscode from 'vscode';
|
||||
// import * as myExtension from '../../extension';
|
||||
|
||||
suite('Extension Test Suite', () => {
|
||||
vscode.window.showInformationMessage('Start all tests.');
|
||||
|
||||
test('Sample test', () => {
|
||||
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
|
||||
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user