-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
98 lines (80 loc) · 2.61 KB
/
Copy pathextension.js
File metadata and controls
98 lines (80 loc) · 2.61 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
const vscode = require('vscode');
const fs = require('fs');
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
setTimeout(() => {
vscode.window.showInformationMessage("Hello, VS Code!");
}, 1000);
const gitExtension = vscode.extensions.getExtension('vscode.git');
if (!gitExtension) { // Just a bit of error checking
vscode.window.showInformationMessage("Git extension not found.");
return;
}
await gitExtension.activate();
const git = gitExtension.exports.getAPI(1);
git.onDidOpenRepository(repo => {
repo.state.onDidChange(() => checkForStagedFiles(repo));
});
git.repositories.forEach(repo => {
repo.state.onDidChange(() => checkForStagedFiles(repo));
});
}
// Function to check if a file is being staged
function checkForStagedFiles(repo) {
const stagedFiles = repo.state.indexChanges.map(change => change.uri.fsPath);// All of the files in hte repo
stagedFiles.forEach(filePath => {
if (filePath.endsWith('.php')) {
commentTestData(filePath, () => {
stageFile(repo, filePath);
});
}
});
}
// Function to comment out lines between "// Start Test" and "// End Test"
function commentTestData(filePath, callback) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
vscode.window.showInformationMessage("Error reading file: ".concat(err));
return;
}
let lines = data.split('\n');
let inTestBlock = false;
let modified = false;
lines = lines.map(line => {
if (line.includes('// Start Test')) {
inTestBlock = true;
}
if (inTestBlock && !line.trim().startsWith('//')) {
line = '// ' + line;
modified = true;
}
if (line.includes('// End Test')) {
inTestBlock = false;
}
return line;
});
if (!modified) {
if (callback) callback();
return;
}
const updatedContent = lines.join('\n');
fs.writeFile(filePath, updatedContent, 'utf8', err => {
if (err) {
console.error("Error writing file:", err);
return;
}
if (callback) callback();
});
});
}
// Function to stage the file after Change
function stageFile(repo, filePath) {
vscode.commands.executeCommand('git.stage', vscode.Uri.file(filePath));
}
function deactivate() {}
module.exports = {
activate,
deactivate
};