Created
February 13, 2019 04:27
-
-
Save madprops/618144596d21c26930bae831ab02fb18 to your computer and use it in GitHub Desktop.
Extension that is "work in progress" because of a vscode bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const vscode = require('vscode') | |
const cl_regex = new RegExp(/^\s*$/, "g") | |
function count_empty_lines() | |
{ | |
let editor = vscode.window.activeTextEditor | |
if(!editor) | |
{ | |
return false | |
} | |
let num_matches = 0 | |
let count = 0 | |
for(let i=0; i < editor.document.lineCount; i++) | |
{ | |
if(editor.document.lineAt(i).text.match(cl_regex)) | |
{ | |
count += 1 | |
} | |
else | |
{ | |
if(count >= 2) | |
{ | |
num_matches += 1 | |
} | |
count = 0 | |
} | |
} | |
let s = "Sets of Multiple Empty Lines were found." | |
let s2 = "Remove Them" | |
if(num_matches === 1) | |
{ | |
s = "Set of Multiple Empty Lines was found." | |
s2 = "Remove It" | |
} | |
if(num_matches > 0) | |
{ | |
vscode.window.showInformationMessage(`${num_matches} ${s}`, "Go", s2) | |
.then(function(selection) | |
{ | |
if(selection === s2) | |
{ | |
remove_empty_lines() | |
} | |
else if(selection === "Go") | |
{ | |
go_to_empty_lines() | |
} | |
}) | |
} | |
else | |
{ | |
vscode.window.showInformationMessage(`${num_matches} ${s}`) | |
} | |
} | |
function go_to_empty_lines() | |
{ | |
let editor = vscode.window.activeTextEditor | |
if(!editor) | |
{ | |
return false | |
} | |
let count = 0 | |
let cline = false | |
for(let i=0; i < editor.document.lineCount; i++) | |
{ | |
let line = editor.document.lineAt(i) | |
if(line.text.match(cl_regex)) | |
{ | |
count += 1 | |
if(count === 2) | |
{ | |
cline = line | |
} | |
} | |
else | |
{ | |
if(count >= 2) | |
{ | |
editor.selection = new vscode.Selection(cline.range.start, cline.range.end) | |
editor.revealRange(cline.range) | |
return true | |
} | |
count = 0 | |
cline = false | |
} | |
} | |
} | |
function remove_empty_lines() | |
{ | |
let editor = vscode.window.activeTextEditor | |
if(!editor) | |
{ | |
return false | |
} | |
let num_matches = 0 | |
let count = 0 | |
let ranges = [] | |
editor.edit(function(builder) | |
{ | |
for(let i=0; i < editor.document.lineCount; i++) | |
{ | |
let line = editor.document.lineAt(i) | |
if(line.text.match(cl_regex)) | |
{ | |
count += 1 | |
if(count >= 2) | |
{ | |
ranges.push(line.rangeIncludingLineBreak) | |
} | |
} | |
else | |
{ | |
if(count >= 2) | |
{ | |
num_matches += 1 | |
for(let range of ranges) | |
{ | |
builder.delete(range) | |
} | |
} | |
count = 0 | |
ranges = [] | |
} | |
} | |
}) | |
let s = "Sets of Multiple Empty Lines were removed." | |
if(num_matches === 1) | |
{ | |
s = "Set of Multiple Empty Lines was removed." | |
} | |
vscode.window.showInformationMessage(`${num_matches} ${s}`) | |
} | |
function activate(context) | |
{ | |
let disposable | |
disposable = vscode.commands.registerCommand('extension.count_empty_lines', function() | |
{ | |
count_empty_lines() | |
}) | |
context.subscriptions.push(disposable) | |
disposable = vscode.commands.registerCommand('extension.remove_empty_lines', function() | |
{ | |
remove_empty_lines() | |
}) | |
context.subscriptions.push(disposable) | |
} | |
exports.activate = activate | |
function deactivate() {} | |
module.exports = | |
{ | |
activate, | |
deactivate | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment