Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Setup what's needed for testing the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoNovaglia committed Apr 3, 2016
1 parent 5594b20 commit e04b1f2
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 157 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
out
node_modules
node_modules
typings
*.log
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "out/src",
"outDir": "${workspaceRoot}/out/src",
"preLaunchTask": "npm"
},
{
Expand All @@ -21,8 +21,8 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "out/test",
"outDir": "${workspaceRoot}/out/test",
"preLaunchTask": "npm"
}
]
}
}
110 changes: 56 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
{
"name": "missinglineendoffile",
"displayName": "Blank Line at the End of File",
"description": "This extension will add a blank line at the end of your files when you save them!",
"version": "0.1.2",
"publisher": "riccardoNovaglia",
"repository": {
"type": "git",
"url": "https://github.com/riccardoNovaglia/vsCodeBlankLine.git"
},
"galleryBanner": {
"color": "#80b3ff"
},
"icon": "images/BlankLineIcon.png",
"engines": {
"vscode": "^0.10.1"
},
"categories": [
"Linters",
"Other"
],
"activationEvents": [
"onCommand:extension.checkBlankLine",
"onCommand:workbench.action.files.save"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "extension.checkBlankLine",
"title": "Check for Blank line at End of File"
}
],
"configuration": {
"type": "object",
"title": "Flag to indicate whether to display an message when a line is added to your file",
"properties": {
"blankLine.showMessage": {
"type": "boolean",
"default": true,
"description": "Controls whether a message is displayed each time a line is added to your file"
}
}
}
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
},
"devDependencies": {
"typescript": "^1.6.2",
"vscode": "0.10.x"
}
}
{
"name": "missinglineendoffile",
"displayName": "Blank Line at the End of File",
"description": "This extension will add a blank line at the end of your files when you save them!",
"version": "0.1.2",
"publisher": "riccardoNovaglia",
"repository": {
"type": "git",
"url": "https://github.com/riccardoNovaglia/vsCodeBlankLine.git"
},
"galleryBanner": {
"color": "#80b3ff"
},
"icon": "images/BlankLineIcon.png",
"engines": {
"vscode": "^0.10.1"
},
"categories": [
"Linters",
"Other"
],
"activationEvents": [
"onCommand:extension.checkBlankLine",
"onCommand:workbench.action.files.save"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "extension.checkBlankLine",
"title": "Check for Blank line at End of File"
}
],
"configuration": {
"type": "object",
"title": "Flag to indicate whether to display an message when a line is added to your file",
"properties": {
"blankLine.showMessage": {
"type": "boolean",
"default": true,
"description": "Controls whether a message is displayed each time a line is added to your file"
}
}
}
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
},
"devDependencies": {
"sinon": "^1.17.3",
"typescript": "^1.6.2",
"vscode": "0.10.x"
},
"dependencies": {}
}
49 changes: 49 additions & 0 deletions src/BlankLineChecker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import VSCodeAdapter from "./VSCodeAdapter";

export default class BlankLineChecker {

private stopThat = "";
private skipOne = false;

private vsAdapter = new VSCodeAdapter();
private shouldDisplayRevertMessage = true;

public addBlankLineIfNeeded(): void {
if (this.shouldNotSkipDoc()) {
this.analyseDocContent();
} else {
if (this.skipOne) {
this.skipOne = false;
}
}
}

private analyseDocContent() {
let checker = this;
if (this.vsAdapter.docLinesCount() > 1 && !this.vsAdapter.lastDocumentLineIsEmpty()) {
this.vsAdapter.addBlankLineAndSaveFile();
if (this.shouldDisplayRevertMessage) {
this.displayRevertMessage();
}
}
}

private displayRevertMessage() {
this.vsAdapter.displayRevertMessage(
(userPressedStopThat) => {
if (userPressedStopThat) {
this.stopThat = this.vsAdapter.docURI();
} else {
this.skipOne = true;
}
this.vsAdapter.revert();
});
}

private shouldNotSkipDoc() {
return this.vsAdapter.docURI() !== this.stopThat && !this.skipOne;
}

dispose() {
}
}
77 changes: 77 additions & 0 deletions src/VSCodeAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { window, commands, workspace, Disposable, ExtensionContext, Range, Position, TextEditor, TextDocument } from 'vscode';
import { EOL } from 'os';

export default class VSCodeAdapter {

private editor;
private doc;
private alertFlag;

private revertButtonLabel = 'Revert!';
private stopThatButtonLabel = 'Stop that!';
private revertMessageLabel = 'A blank line has been added at the end of your file!';

public constructor() {
this.init();
}

private init() {
this.alertFlag = workspace.getConfiguration("blankLine").get('showMessage');
this.doc = this.editor.document;
this.editor = window.activeTextEditor;
}

public lastDocumentLineIsEmpty(): boolean {
return this.lastTextLine() !== "";
}

private lastTextLine() {
return this.doc.lineAt(this.doc.lineCount - 1).text;
}

public docLinesCount(): number {
return this.doc.lineCount;
}

public docURI(): string {
return this.doc.uri.toString();
}

public alertConfigValue(): boolean {
return this.alertFlag !== {};
}

public addBlankLineAndSaveFile() {
this.editor.edit(function(editbuilder) {
editbuilder.insert(new Position(this.doc.lineCount, this.lastTextLine().length), EOL);
setTimeout(function() {
this.doc.save();
}, 200);
})
}

public displayRevertMessage(callback) {
window
.showInformationMessage(this.revertMessageLabel, this.revertButtonLabel, this.stopThatButtonLabel)
.then(buttonPressedValue => {
callback(buttonPressedValue === this.stopThatButtonLabel);
});
}

public revert() {
this.editor.edit(function(editbuilder) {
// TODO: improve
var lastLine = this.doc.lineCount - 1;
var penultimateLine = this.doc.lineCount - 2;
var secondlastLine = this.doc.lineAt(penultimateLine);
var secondLastLineText = secondlastLine.text;
var deleteRange = new Range(new Position(penultimateLine, secondLastLineText.length), new Position(lastLine, 1));

editbuilder.delete(deleteRange);
setTimeout(function() {
this.doc.save();
}, 200);
});
}

}
85 changes: 2 additions & 83 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,34 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the necessary extensibility types to use in your code below
import { window, commands, workspace, MessageItem, Disposable, ExtensionContext, Range, Position, Location } from 'vscode';
import { window, commands, workspace, Disposable, ExtensionContext, Range, Position } from 'vscode';
import { EOL } from 'os';
import vscode = require('vscode');
import BlankLineChecker from './BlankLineChecker';


// This method is called when your extension is activated. Activation is
// controlled by the activation events defined in package.json.
export function activate(context: ExtensionContext) {

// 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 "BlankLineChecker" is now active!');

// reate a new object which will verify that the last line is blank, and will add one if neededCcreate a new object which will verify that the last line is blank, and will add one if needed
let blankLineChecker = new BlankLineChecker();
// controller
let controller = new BlankCheckerController(blankLineChecker);

var disposable = commands.registerCommand('extension.checkBlankLine', () => {
blankLineChecker.addBlankLineIfNeeded();
});

// Add to a list of disposables which are disposed when this extension is deactivated.
context.subscriptions.push(controller);
context.subscriptions.push(blankLineChecker);
context.subscriptions.push(disposable);
}

class BlankLineChecker {

private stopThat = "";
private skipOne = false;

public addBlankLineIfNeeded() {
// Get the current text editor
let editor = window.activeTextEditor;
let checker = this;
let doc = editor.document;
if (doc.uri.toString() != this.stopThat && !this.skipOne) {
var lastLine = doc.lineAt(doc.lineCount - 1);
var lastLineText = lastLine.text
if (doc.lineCount > 1 && lastLineText !== '') {
editor.edit(function(editbuilder) {
editbuilder.insert(new Position(doc.lineCount, lastLineText.length), EOL);
setTimeout(function() {
doc.save();
}, 200);
let alertFlag = workspace.getConfiguration("blankLine").get('showMessage');
if (alertFlag) {
var revertButton = 'Revert!';
var stopThat = 'Stop that!';
window.showInformationMessage('A blank line has been added at the end of your file!', revertButton, stopThat).then(value => {
if (value === revertButton) {
checker.revert(editor, checker, false);
} else if (value === stopThat) {
checker.revert(editor, checker, true);
}
});
}
})
}
} else {
if (this.skipOne) {
this.skipOne = false;
}
}
}

private revert(editor: vscode.TextEditor, checker: BlankLineChecker, stopIt) {
let doc = editor.document;
editor.edit(function(editbuilder) {
var lastLine = doc.lineCount - 1;
var penultimateLine = doc.lineCount - 2;
var secondlastLine = doc.lineAt(penultimateLine);
var secondLastLineText = secondlastLine.text;
var deleteRange = new Range(new Position(penultimateLine, secondLastLineText.length), new Position(lastLine, 1));

editbuilder.delete(deleteRange);
setTimeout(function() {
doc.save();
}, 200);
if (stopIt) {
checker.stopThat = doc.uri.toString();
} else {
checker.skipOne = true;
}
});
}

dispose() {

}
}

class BlankCheckerController {

private blankChecker: BlankLineChecker;

constructor(blankChecker: BlankLineChecker) {
this.blankChecker = blankChecker;

// subscribe to trigger when the file is saved
let subscriptions: Disposable[] = [];
workspace.onDidSaveTextDocument(this._onEvent, this, subscriptions);

// create a combined disposable from both event subscriptions
}

dispose() {

}

private _onEvent() {
Expand Down
Loading

0 comments on commit e04b1f2

Please sign in to comment.