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

Commit e04b1f2

Browse files
Setup what's needed for testing the extension
1 parent 5594b20 commit e04b1f2

File tree

8 files changed

+238
-157
lines changed

8 files changed

+238
-157
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
out
2-
node_modules
2+
node_modules
3+
typings
4+
*.log

.vscode/launch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
1111
"stopOnEntry": false,
1212
"sourceMaps": true,
13-
"outDir": "out/src",
13+
"outDir": "${workspaceRoot}/out/src",
1414
"preLaunchTask": "npm"
1515
},
1616
{
@@ -21,8 +21,8 @@
2121
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
2222
"stopOnEntry": false,
2323
"sourceMaps": true,
24-
"outDir": "out/test",
24+
"outDir": "${workspaceRoot}/out/test",
2525
"preLaunchTask": "npm"
2626
}
2727
]
28-
}
28+
}

package.json

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
1-
{
2-
"name": "missinglineendoffile",
3-
"displayName": "Blank Line at the End of File",
4-
"description": "This extension will add a blank line at the end of your files when you save them!",
5-
"version": "0.1.2",
6-
"publisher": "riccardoNovaglia",
7-
"repository": {
8-
"type": "git",
9-
"url": "https://github.com/riccardoNovaglia/vsCodeBlankLine.git"
10-
},
11-
"galleryBanner": {
12-
"color": "#80b3ff"
13-
},
14-
"icon": "images/BlankLineIcon.png",
15-
"engines": {
16-
"vscode": "^0.10.1"
17-
},
18-
"categories": [
19-
"Linters",
20-
"Other"
21-
],
22-
"activationEvents": [
23-
"onCommand:extension.checkBlankLine",
24-
"onCommand:workbench.action.files.save"
25-
],
26-
"main": "./out/src/extension",
27-
"contributes": {
28-
"commands": [
29-
{
30-
"command": "extension.checkBlankLine",
31-
"title": "Check for Blank line at End of File"
32-
}
33-
],
34-
"configuration": {
35-
"type": "object",
36-
"title": "Flag to indicate whether to display an message when a line is added to your file",
37-
"properties": {
38-
"blankLine.showMessage": {
39-
"type": "boolean",
40-
"default": true,
41-
"description": "Controls whether a message is displayed each time a line is added to your file"
42-
}
43-
}
44-
}
45-
},
46-
"scripts": {
47-
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
48-
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
49-
},
50-
"devDependencies": {
51-
"typescript": "^1.6.2",
52-
"vscode": "0.10.x"
53-
}
54-
}
1+
{
2+
"name": "missinglineendoffile",
3+
"displayName": "Blank Line at the End of File",
4+
"description": "This extension will add a blank line at the end of your files when you save them!",
5+
"version": "0.1.2",
6+
"publisher": "riccardoNovaglia",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/riccardoNovaglia/vsCodeBlankLine.git"
10+
},
11+
"galleryBanner": {
12+
"color": "#80b3ff"
13+
},
14+
"icon": "images/BlankLineIcon.png",
15+
"engines": {
16+
"vscode": "^0.10.1"
17+
},
18+
"categories": [
19+
"Linters",
20+
"Other"
21+
],
22+
"activationEvents": [
23+
"onCommand:extension.checkBlankLine",
24+
"onCommand:workbench.action.files.save"
25+
],
26+
"main": "./out/src/extension",
27+
"contributes": {
28+
"commands": [
29+
{
30+
"command": "extension.checkBlankLine",
31+
"title": "Check for Blank line at End of File"
32+
}
33+
],
34+
"configuration": {
35+
"type": "object",
36+
"title": "Flag to indicate whether to display an message when a line is added to your file",
37+
"properties": {
38+
"blankLine.showMessage": {
39+
"type": "boolean",
40+
"default": true,
41+
"description": "Controls whether a message is displayed each time a line is added to your file"
42+
}
43+
}
44+
}
45+
},
46+
"scripts": {
47+
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
48+
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
49+
},
50+
"devDependencies": {
51+
"sinon": "^1.17.3",
52+
"typescript": "^1.6.2",
53+
"vscode": "0.10.x"
54+
},
55+
"dependencies": {}
56+
}

src/BlankLineChecker.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import VSCodeAdapter from "./VSCodeAdapter";
2+
3+
export default class BlankLineChecker {
4+
5+
private stopThat = "";
6+
private skipOne = false;
7+
8+
private vsAdapter = new VSCodeAdapter();
9+
private shouldDisplayRevertMessage = true;
10+
11+
public addBlankLineIfNeeded(): void {
12+
if (this.shouldNotSkipDoc()) {
13+
this.analyseDocContent();
14+
} else {
15+
if (this.skipOne) {
16+
this.skipOne = false;
17+
}
18+
}
19+
}
20+
21+
private analyseDocContent() {
22+
let checker = this;
23+
if (this.vsAdapter.docLinesCount() > 1 && !this.vsAdapter.lastDocumentLineIsEmpty()) {
24+
this.vsAdapter.addBlankLineAndSaveFile();
25+
if (this.shouldDisplayRevertMessage) {
26+
this.displayRevertMessage();
27+
}
28+
}
29+
}
30+
31+
private displayRevertMessage() {
32+
this.vsAdapter.displayRevertMessage(
33+
(userPressedStopThat) => {
34+
if (userPressedStopThat) {
35+
this.stopThat = this.vsAdapter.docURI();
36+
} else {
37+
this.skipOne = true;
38+
}
39+
this.vsAdapter.revert();
40+
});
41+
}
42+
43+
private shouldNotSkipDoc() {
44+
return this.vsAdapter.docURI() !== this.stopThat && !this.skipOne;
45+
}
46+
47+
dispose() {
48+
}
49+
}

src/VSCodeAdapter.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { window, commands, workspace, Disposable, ExtensionContext, Range, Position, TextEditor, TextDocument } from 'vscode';
2+
import { EOL } from 'os';
3+
4+
export default class VSCodeAdapter {
5+
6+
private editor;
7+
private doc;
8+
private alertFlag;
9+
10+
private revertButtonLabel = 'Revert!';
11+
private stopThatButtonLabel = 'Stop that!';
12+
private revertMessageLabel = 'A blank line has been added at the end of your file!';
13+
14+
public constructor() {
15+
this.init();
16+
}
17+
18+
private init() {
19+
this.alertFlag = workspace.getConfiguration("blankLine").get('showMessage');
20+
this.doc = this.editor.document;
21+
this.editor = window.activeTextEditor;
22+
}
23+
24+
public lastDocumentLineIsEmpty(): boolean {
25+
return this.lastTextLine() !== "";
26+
}
27+
28+
private lastTextLine() {
29+
return this.doc.lineAt(this.doc.lineCount - 1).text;
30+
}
31+
32+
public docLinesCount(): number {
33+
return this.doc.lineCount;
34+
}
35+
36+
public docURI(): string {
37+
return this.doc.uri.toString();
38+
}
39+
40+
public alertConfigValue(): boolean {
41+
return this.alertFlag !== {};
42+
}
43+
44+
public addBlankLineAndSaveFile() {
45+
this.editor.edit(function(editbuilder) {
46+
editbuilder.insert(new Position(this.doc.lineCount, this.lastTextLine().length), EOL);
47+
setTimeout(function() {
48+
this.doc.save();
49+
}, 200);
50+
})
51+
}
52+
53+
public displayRevertMessage(callback) {
54+
window
55+
.showInformationMessage(this.revertMessageLabel, this.revertButtonLabel, this.stopThatButtonLabel)
56+
.then(buttonPressedValue => {
57+
callback(buttonPressedValue === this.stopThatButtonLabel);
58+
});
59+
}
60+
61+
public revert() {
62+
this.editor.edit(function(editbuilder) {
63+
// TODO: improve
64+
var lastLine = this.doc.lineCount - 1;
65+
var penultimateLine = this.doc.lineCount - 2;
66+
var secondlastLine = this.doc.lineAt(penultimateLine);
67+
var secondLastLineText = secondlastLine.text;
68+
var deleteRange = new Range(new Position(penultimateLine, secondLastLineText.length), new Position(lastLine, 1));
69+
70+
editbuilder.delete(deleteRange);
71+
setTimeout(function() {
72+
this.doc.save();
73+
}, 200);
74+
});
75+
}
76+
77+
}

src/extension.ts

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,34 @@
1-
// The module 'vscode' contains the VS Code extensibility API
2-
// Import the necessary extensibility types to use in your code below
3-
import { window, commands, workspace, MessageItem, Disposable, ExtensionContext, Range, Position, Location } from 'vscode';
1+
import { window, commands, workspace, Disposable, ExtensionContext, Range, Position } from 'vscode';
42
import { EOL } from 'os';
53
import vscode = require('vscode');
4+
import BlankLineChecker from './BlankLineChecker';
65

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

12-
// Use the console to output diagnostic information (console.log) and errors (console.error).
13-
// This line of code will only be executed once when your extension is activated.
14-
// console.log('Congratulations, your extension "BlankLineChecker" is now active!');
15-
16-
// 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
178
let blankLineChecker = new BlankLineChecker();
18-
// controller
199
let controller = new BlankCheckerController(blankLineChecker);
2010

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

25-
// Add to a list of disposables which are disposed when this extension is deactivated.
2615
context.subscriptions.push(controller);
2716
context.subscriptions.push(blankLineChecker);
2817
context.subscriptions.push(disposable);
2918
}
3019

31-
class BlankLineChecker {
32-
33-
private stopThat = "";
34-
private skipOne = false;
35-
36-
public addBlankLineIfNeeded() {
37-
// Get the current text editor
38-
let editor = window.activeTextEditor;
39-
let checker = this;
40-
let doc = editor.document;
41-
if (doc.uri.toString() != this.stopThat && !this.skipOne) {
42-
var lastLine = doc.lineAt(doc.lineCount - 1);
43-
var lastLineText = lastLine.text
44-
if (doc.lineCount > 1 && lastLineText !== '') {
45-
editor.edit(function(editbuilder) {
46-
editbuilder.insert(new Position(doc.lineCount, lastLineText.length), EOL);
47-
setTimeout(function() {
48-
doc.save();
49-
}, 200);
50-
let alertFlag = workspace.getConfiguration("blankLine").get('showMessage');
51-
if (alertFlag) {
52-
var revertButton = 'Revert!';
53-
var stopThat = 'Stop that!';
54-
window.showInformationMessage('A blank line has been added at the end of your file!', revertButton, stopThat).then(value => {
55-
if (value === revertButton) {
56-
checker.revert(editor, checker, false);
57-
} else if (value === stopThat) {
58-
checker.revert(editor, checker, true);
59-
}
60-
});
61-
}
62-
})
63-
}
64-
} else {
65-
if (this.skipOne) {
66-
this.skipOne = false;
67-
}
68-
}
69-
}
70-
71-
private revert(editor: vscode.TextEditor, checker: BlankLineChecker, stopIt) {
72-
let doc = editor.document;
73-
editor.edit(function(editbuilder) {
74-
var lastLine = doc.lineCount - 1;
75-
var penultimateLine = doc.lineCount - 2;
76-
var secondlastLine = doc.lineAt(penultimateLine);
77-
var secondLastLineText = secondlastLine.text;
78-
var deleteRange = new Range(new Position(penultimateLine, secondLastLineText.length), new Position(lastLine, 1));
79-
80-
editbuilder.delete(deleteRange);
81-
setTimeout(function() {
82-
doc.save();
83-
}, 200);
84-
if (stopIt) {
85-
checker.stopThat = doc.uri.toString();
86-
} else {
87-
checker.skipOne = true;
88-
}
89-
});
90-
}
91-
92-
dispose() {
93-
94-
}
95-
}
96-
9720
class BlankCheckerController {
9821

9922
private blankChecker: BlankLineChecker;
10023

10124
constructor(blankChecker: BlankLineChecker) {
10225
this.blankChecker = blankChecker;
10326

104-
// subscribe to trigger when the file is saved
10527
let subscriptions: Disposable[] = [];
10628
workspace.onDidSaveTextDocument(this._onEvent, this, subscriptions);
107-
108-
// create a combined disposable from both event subscriptions
10929
}
11030

11131
dispose() {
112-
11332
}
11433

11534
private _onEvent() {

0 commit comments

Comments
 (0)