diff --git a/CHANGELOG.md b/CHANGELOG.md index e915bdd..373b1f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.5.1 + +## What's New +* Adds an `advancedNewFile.showInformationMessages` configuration option to + control whether top-bar notifications display. Enabled by default. + # 0.5.0 ## What's New diff --git a/README.md b/README.md index 56beaf1..6eadcd9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ files anywhere in your workspace. * Additional option of adding `advancedNewFile.exclude` settings to workspace settings just like native `files.exlude` except it explicitly effects AdvancedNewFile plugin only. (thanks to [Kaffiend](https://github.com/Kaffiend)) ## Configuration Example + ``` "advancedNewFile": { "exclude": { @@ -24,9 +25,11 @@ files anywhere in your workspace. "node_modules_electron": true, "dev": true, "dist": true - } + }, + "showInformationMessages": false } ``` + ## Usage * Command palette: "Advanced New File" diff --git a/package.json b/package.json index 2c1562a..2953ba3 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,24 @@ "key": "alt+ctrl+n", "mac": "alt+cmd+n" } - ] + ], + "configuration": { + "type": "object", + "title": "AdvancedNewFile configuration", + "properties": { + "advancedNewFile.exclude": { + "type": ["object", "null"], + "additionalProperties": { "type": "boolean" }, + "default": null, + "description": "Directories to ignore in auto-complete" + }, + "advancedNewFile.showInformationMessages": { + "type": "boolean", + "default": true, + "description": "Control whether top-bar notifications display" + } + } + } }, "scripts": { "vscode:prepublish": "tsc -p ./", diff --git a/src/extension.ts b/src/extension.ts index 00b1dc0..241891b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -143,7 +143,9 @@ export function createFileOrFolder(absolutePath: string): string { export function openFile(absolutePath: string): PromiseLike { if (isFolderDescriptor(absolutePath)) { - vscode.window.showInformationMessage(`Folder created: ${absolutePath}`); + if (vscode.workspace.getConfiguration('advancedNewFile').get('showInformationMessages', true)) { + vscode.window.showInformationMessage(`Folder created: ${absolutePath}`); + } return Promise.resolve(absolutePath); } diff --git a/test/extension.test.ts b/test/extension.test.ts index bfd6765..f74616c 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -280,6 +280,19 @@ describe('Advanced New File', () => { }); describe('openFile', () => { + const mockGetConfiguration = function(config = { showInformationMessages: true }) { + return function(name) { + switch (name) { + case 'advancedNewFile': + return { + get: (configName) => config[configName] + }; + default: + return {}; + } + }; + }; + it('attempts to open the file', () => { const textDocument = 'mock document'; const openTextDocument = chai.spy(() => Promise.resolve(textDocument)); @@ -287,7 +300,10 @@ describe('Advanced New File', () => { const advancedNewFile = proxyquire('../src/extension', { vscode: { - workspace: { openTextDocument }, + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration() + }, window: { showTextDocument } } }); @@ -305,7 +321,10 @@ describe('Advanced New File', () => { const advancedNewFile = proxyquire('../src/extension', { vscode: { - workspace: { openTextDocument }, + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration() + }, window: { showTextDocument } } }); @@ -322,7 +341,10 @@ describe('Advanced New File', () => { const advancedNewFile = proxyquire('../src/extension', { vscode: { - workspace: { openTextDocument }, + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration() + }, window: { showTextDocument } } }); @@ -340,7 +362,10 @@ describe('Advanced New File', () => { const advancedNewFile = proxyquire('../src/extension', { vscode: { - workspace: { openTextDocument }, + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration() + }, window: { showTextDocument } } }); @@ -349,6 +374,73 @@ describe('Advanced New File', () => { .to.eventually.be.rejectedWith('Could not open document'); }); }); + + context('file is a folder', () => { + it('does not attempt to open it', () => { + const openTextDocument = chai.spy(); + const showInformationMessage = chai.spy(); + + const advancedNewFile = proxyquire('../src/extension', { + vscode: { + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration() + }, + window: { + showInformationMessage + } + } + }); + + advancedNewFile.openFile('/path/to/folder/').then(() => { + expect(openTextDocument).not.to.have.been.called(); + }); + }); + + it('displays an informational message instead', () => { + const openTextDocument = chai.spy(); + const showInformationMessage = chai.spy(); + + const advancedNewFile = proxyquire('../src/extension', { + vscode: { + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration() + }, + window: { + showInformationMessage + } + } + }); + + advancedNewFile.openFile('/path/to/folder/').then(() => { + expect(showInformationMessage).to.have.been.called.with('Folder created: /path/to/folder/'); + }); + }); + + context('informational messages disabled in config', () => { + it('does not display an informational message', () => { + const openTextDocument = chai.spy(); + const showInformationMessage = chai.spy(); + + const advancedNewFile = proxyquire('../src/extension', { + vscode: { + workspace: { + openTextDocument, + getConfiguration: mockGetConfiguration({ showInformationMessages: false }) + }, + window: { + showInformationMessage + } + } + }); + + advancedNewFile.openFile('/path/to/folder/').then(() => { + expect(showInformationMessage).not.to.have.been.called(); + }); + }); + }); + }); }); describe('cacheSelection', () => { @@ -584,7 +676,7 @@ describe('Advanced New File', () => { switch (name) { case 'advancedNewFile': return { - get: () => {} + get: (name, defaultValue) => defaultValue }; default: return {}; @@ -633,6 +725,77 @@ describe('Advanced New File', () => { }); }); + context('info messages disabled in settings', () => { + it('creates a folder at given path and does not show information ' + + 'message', () => { + let command; + const registerCommand = (name, commandFn) => command = commandFn; + + const textDocument = 'mock document'; + const openTextDocument = chai.spy(() => Promise.resolve(textDocument)); + const showTextDocument = chai.spy(); + const showErrorMessage = chai.spy(); + const showInformationMessage = chai.spy(); + + const advancedNewFile = proxyquire('../src/extension', { + vscode: { + commands: { registerCommand }, + workspace: { + rootPath: tmpDir, + openTextDocument, + getConfiguration(name) { + switch (name) { + case 'advancedNewFile': + return { + get: (name, defaultValue) => false + }; + default: + return {}; + } + } + }, + window: { + showErrorMessage, + showQuickPick: () => Promise.resolve({ label: 'path/to' }), + showInputBox: () => Promise.resolve('input/path/to/folder/'), + showInformationMessage, + showTextDocument + } + }, + fs: { + statSync: () => { + return { isDirectory: () => true }; + } + }, + 'vscode-cache': class Cache { + get() {} + has() { return false } + put() {} + } + }); + + const context = { subscriptions: [] }; + + advancedNewFile.activate(context); + + const newFolderDescriptor = + path.join(tmpDir, 'path/to/input/path/to/folder/'); + + return command().then(() => { + expect(openTextDocument) + .to.not.have.been.called.with(newFolderDescriptor); + + expect(showTextDocument) + .to.not.have.been.called.with(textDocument); + + expect(showInformationMessage).to.not.have.been.called(); + + expect(fs.statSync(newFolderDescriptor).isDirectory()) + .to.be.true; + }); + }); + }); + context('no project opened in workspace', () => { it('shows an error message', () => { let command;