Skip to content

Commit

Permalink
[Feature] Support regex match
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Fang committed Dec 18, 2017
1 parent 92401ef commit 90384ed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Place your settings in this file to overwrite default and user settings.
{
"standard.enable": false
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Check duplicate lines in the file content.

`ctrl+shift+p` Commands:
- `Check Duplicates`: Check duplicate lines in file immediately (trim empty characters only)
- `Check Duplicates With Trim Condition`: Check duplicate lines in file (trim both empty and customer input characters)
- `Check Duplicates With Trim Condition`: Check duplicate lines in file (trim both empty and customer input characters)
- `Check Duplicates With Regex Match`: Check duplicate lines in file using customer regex (trim empty characters and then compare regex matched string in each line)
43 changes: 36 additions & 7 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ function activate(context) {
// 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('extension.checkDupWithTrim', function () {
let disposable = vscode.commands.registerCommand('extension.checkDup', 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(`${dupLines.length} duplicates found.`);
checkDup();
});

context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('extension.checkDupWithTrim', function () {
vscode.window.showInputBox({
prompt: 'Characters to trim'
}).then(input => {
Expand All @@ -30,25 +35,49 @@ function activate(context) {

context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('extension.checkDup', function () {
checkDup();
disposable = vscode.commands.registerCommand('extension.checkDupWithRegex', function () {
vscode.window.showInputBox({
prompt: 'RegExp to match and selet for each line'
}).then(input => {
if (input === undefined) return;
checkDup('', input);
})
});

context.subscriptions.push(disposable);

function checkDup(trimChars) {
function checkDup(trimChars, regex) {
let doc = vscode.window.activeTextEditor.document;
const text = doc.getText();
const lines = text.split('\r\n').map(line => _.trim(line.trim(), trimChars)).filter(line => !_.isEmpty(line));
let lines = text.split('\r\n').map(line => line.trim());

if (!_.isEmpty(trimChars)) {
lines = lines.map(line => _.trim(line, trimChars));
}

if (!_.isEmpty(regex)) {
regex = _.trim(regex, '/');
const re = new RegExp(regex);
if (!re) return vscode.window.showErrorMessage(`[Invalid Regex]: ${regex}`);
lines = lines.map(line => {
const match = re.exec(line)
return match ? match[match.length - 1] : '';
})
}

lines = lines.filter(line => !_.isEmpty(line));
const dupLines = []
lines.forEach((line, i) => {
if (dupLines.indexOf(line) === -1 && lines.indexOf(line, i + 1) >= 0) {
dupLines.push(line)
}
});

const trimInfo = _.isEmpty(trimChars) ? '' : ` (trim: ${trimChars})`;
const regexInfo = _.isEmpty(regex) ? '' : ` (regex: /${regex}/)`;
output.clear();
output.show();
output.appendLine(`${dupLines.length} duplicates found${_.isEmpty(trimChars) ? '' : ' (trim: ' + trimChars + ')'}:`);
output.appendLine(`${dupLines.length} duplicates found${trimInfo}${regexInfo}:`);
dupLines.forEach(line => output.appendLine(line));
}
}
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/jianbingfang/vscode-dup-checker.git"
},
"homepage": "https://github.com/jianbingfang/vscode-dup-checker",
"version": "0.0.1",
"version": "0.0.2",
"author": {
"name": "Jianbing Fang",
"email": "[email protected]"
Expand All @@ -28,7 +28,8 @@
],
"activationEvents": [
"onCommand:extension.checkDup",
"onCommand:extension.checkDupWithTrim"
"onCommand:extension.checkDupWithTrim",
"onCommand:extension.checkDupWithRegex"
],
"main": "./extension",
"contributes": {
Expand All @@ -40,6 +41,10 @@
{
"command": "extension.checkDupWithTrim",
"title": "Check Duplicates With Trim Condition"
},
{
"command": "extension.checkDupWithRegex",
"title": "Check Duplicates With Regex Match"
}
]
},
Expand Down

0 comments on commit 90384ed

Please sign in to comment.