This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup what's needed for testing the extension
- Loading branch information
1 parent
5594b20
commit e04b1f2
Showing
8 changed files
with
238 additions
and
157 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
out | ||
node_modules | ||
node_modules | ||
typings | ||
*.log |
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
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
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": {} | ||
} |
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
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() { | ||
} | ||
} |
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
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); | ||
}); | ||
} | ||
|
||
} |
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
Oops, something went wrong.