From 51f769a017ee6bd6c0cd6b88efd499458e4cb1c0 Mon Sep 17 00:00:00 2001 From: Seongjae Lee Date: Mon, 28 Sep 2015 01:14:39 -0700 Subject: [PATCH] Apply the interlink extended grammar only on valid notes To do so, we need to activate the package on Atom startup. --- grammars/nvatom.cson | 13 +---- lib/interlink.coffee | 3 ++ lib/notational-velocity.coffee | 10 ++-- package.json | 6 --- spec/interlink-spec.coffee | 73 ++++++++++++++++------------ spec/notational-velocity-spec.coffee | 34 ++++++------- 6 files changed, 65 insertions(+), 74 deletions(-) diff --git a/grammars/nvatom.cson b/grammars/nvatom.cson index de93f9d..be0be65 100644 --- a/grammars/nvatom.cson +++ b/grammars/nvatom.cson @@ -1,14 +1,5 @@ -'name': 'nvAtom Markdown' -'scopeName': 'source.gfm' -'fileTypes': [ - 'markdown' - 'md' - 'mdown' - 'mkd' - 'mkdown' - 'rmd' - 'ron' -] +'name': 'nvAtom Github Markdown' +'scopeName': 'source.gfm.nvatom' 'patterns': [ { 'include': 'source.gfm' diff --git a/lib/interlink.coffee b/lib/interlink.coffee index b66a5af..1c8e8de 100644 --- a/lib/interlink.coffee +++ b/lib/interlink.coffee @@ -7,6 +7,9 @@ class Interlink constructor: -> @subscriptions = new CompositeDisposable @subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => Interlink.openInterlink() + @subscriptions.add atom.workspace.observeTextEditors (editor) -> + if Utility.isNote(editor.getPath()) + editor.setGrammar(atom.grammars.grammarForScopeName('source.gfm.nvatom')) destroy: -> @subscriptions.dispose() diff --git a/lib/notational-velocity.coffee b/lib/notational-velocity.coffee index 762b11c..049a2c0 100644 --- a/lib/notational-velocity.coffee +++ b/lib/notational-velocity.coffee @@ -24,16 +24,12 @@ module.exports = type: 'boolean' default: true - notationalVelocityView: null activate: (state) -> @rootDirectory = @ensureNoteDirectory() - # Events subscribed to in atom's system can be easily cleaned up with a - # CompositeDisposable @subscriptions = new CompositeDisposable - # Register command that toggles this view @subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:toggle': => @createView(state).toggle() @@ -55,11 +51,11 @@ module.exports = deactivate: -> @subscriptions.dispose() - @interlnk.destroy() - @notationalVelocityView.destroy() + @interlnk?.destroy() + @notationalVelocityView?.destroy() serialize: -> - notationalVelocityViewState: @notationalVelocityView.serialize() + notationalVelocityViewState: @notationalVelocityView?.serialize() createView: (state, docQuery) -> unless @notationalVelocityView? diff --git a/package.json b/package.json index ed36aea..c5b86f4 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,6 @@ "Jonathan Hoyt " ], "description": "Notational Velocity for Atom", - "activationCommands": { - "atom-workspace": [ - "nvatom:toggle", - "nvatom:openInterlink" - ] - }, "keywords": [ "wiki", "notational velocity", diff --git a/spec/interlink-spec.coffee b/spec/interlink-spec.coffee index 37acd1e..2b11756 100644 --- a/spec/interlink-spec.coffee +++ b/spec/interlink-spec.coffee @@ -9,14 +9,12 @@ describe 'Interlink', -> beforeEach -> workspaceElement = atom.views.getView(atom.workspace) - activationPromise = atom.packages.activatePackage('nvatom') noteDirectory = temp.mkdirSync() atom.config.set('nvatom.directory', noteDirectory) - atom.commands.dispatch workspaceElement, 'nvatom:toggle' waitsForPromise -> - activationPromise + atom.packages.activatePackage('nvatom') afterEach -> atom.config.set('nvatom.directory', defaultDirectory) @@ -24,39 +22,50 @@ describe 'Interlink', -> describe 'when getInerlinkUnderCursor is called', -> editor = null - beforeEach -> - waitsForPromise -> - atom.workspace.open(path.join(noteDirectory, 'Interlink.md')).then (o) -> editor = o + describe 'under the note directory', -> + beforeEach -> + waitsForPromise -> + atom.workspace.open(path.join(noteDirectory, 'Interlink.md')).then (o) -> editor = o - it 'returns a trimmed interlink text', -> - testdata = [ - { position: [0, 2], text: '[[Car]]', expected: 'Car' }, - { position: [0, 2], text: '[[Notational Velocity]]', expected: 'Notational Velocity' }, - { position: [0, 2], text: '[[한글 Alphabet Test]]', expected: '한글 Alphabet Test' }, - { position: [0, 2], text: '[[ Car ]]', expected: 'Car' }, - { position: [0, 2], text: '[[Car/Mini]]', expected: 'Car/Mini' }, - ] + it 'returns a trimmed interlink text', -> + testdata = [ + { position: [0, 2], text: '[[Car]]', expected: 'Car' }, + { position: [0, 2], text: '[[Notational Velocity]]', expected: 'Notational Velocity' }, + { position: [0, 2], text: '[[한글 Alphabet Test]]', expected: '한글 Alphabet Test' }, + { position: [0, 2], text: '[[ Car ]]', expected: 'Car' }, + { position: [0, 2], text: '[[Car/Mini]]', expected: 'Car/Mini' }, + ] - for testitem in testdata - editor.setText testitem.text - editor.setCursorBufferPosition testitem.position - expect(Interlink.getInterlinkUnderCursor(editor)).toBe testitem.expected + for testitem in testdata + editor.setText testitem.text + editor.setCursorBufferPosition testitem.position + expect(Interlink.getInterlinkUnderCursor(editor)).toBe testitem.expected - it 'returns undefined for invalid text', -> - testdata = [ - { position: [0, 2], text: '[[]]' }, - { position: [0, 3], text: '[[]]' }, - { position: [0, 2], text: '[[ ]]' }, - { position: [0, 1], text: '[Car]' }, - { position: [0, 2], text: '[[[Car]]]' }, - { position: [0, 2], text: '[[Car]' }, - { position: [0, 2], text: '[[Car]]]' }, - { position: [0, 1], text: 'Car' }, - ] + it 'returns undefined for invalid text', -> + testdata = [ + { position: [0, 2], text: '[[]]' }, + { position: [0, 3], text: '[[]]' }, + { position: [0, 2], text: '[[ ]]' }, + { position: [0, 1], text: '[Car]' }, + { position: [0, 2], text: '[[[Car]]]' }, + { position: [0, 2], text: '[[Car]' }, + { position: [0, 2], text: '[[Car]]]' }, + { position: [0, 1], text: 'Car' }, + ] - for testitem in testdata - editor.setText testitem.text - editor.setCursorBufferPosition testitem.position + for testitem in testdata + editor.setText testitem.text + editor.setCursorBufferPosition testitem.position + expect(Interlink.getInterlinkUnderCursor(editor)).toBe undefined + + describe 'under a random directory', -> + beforeEach -> + waitsForPromise -> + atom.workspace.open(path.join(temp.mkdirSync(), 'Interlink.md')).then (o) -> editor = o + + it 'does not apply the grammar', -> + editor.setText '[[Car]]' + editor.setCursorBufferPosition [0, 2] expect(Interlink.getInterlinkUnderCursor(editor)).toBe undefined describe 'when openInterlink is called', -> diff --git a/spec/notational-velocity-spec.coffee b/spec/notational-velocity-spec.coffee index ec9a75f..bd0c268 100644 --- a/spec/notational-velocity-spec.coffee +++ b/spec/notational-velocity-spec.coffee @@ -1,4 +1,5 @@ path = require 'path' +temp = require 'temp' describe "nvAtom", -> defaultDirectory = atom.config.get('nvatom.directory') @@ -7,41 +8,38 @@ describe "nvAtom", -> beforeEach -> workspaceElement = atom.views.getView(atom.workspace) - activationPromise = atom.packages.activatePackage('nvatom') - atom.config.set('nvatom.directory', 'testdata') afterEach -> atom.config.set('nvatom.directory', defaultDirectory) describe "when the nvatom:toggle event is triggered", -> it "attaches and then detaches the view", -> - expect(workspaceElement.querySelector('.nvatom')).not.toExist() - - # This is an activation event, triggering it will cause the package to be activated. - atom.commands.dispatch workspaceElement, 'nvatom:toggle' - + noteDirectory = path.join(temp.mkdirSync()) + atom.config.set('nvatom.directory', noteDirectory) + waitsForPromise -> - activationPromise + atom.packages.activatePackage('nvatom') runs -> - expect(workspaceElement.querySelector('.nvatom')).toExist() + expect(workspaceElement.querySelector('.nvatom')).not.toExist() + atom.commands.dispatch workspaceElement, 'nvatom:toggle' + expect(workspaceElement.querySelector('.nvatom')).toExist() + expect(workspaceElement.querySelector('.nvatom').parentNode.style.display).not.toBe 'none' + + atom.commands.dispatch workspaceElement, 'nvatom:toggle' + expect(workspaceElement.querySelector('.nvatom').parentNode.style.display).toBe 'none' it "checks if we banned the default directory under packages directory", -> - atom.notifications.clear() + noteDirectory = path.join(process.env.ATOM_HOME, 'packages', 'nvatom', 'notebook') + atom.config.set('nvatom.directory', noteDirectory) waitsForPromise -> - atom.packages.activatePackage('notifications') + atom.packages.activatePackage('nvatom') runs -> - noteDirectoryUnderPackageDirectory = path.join(process.env.ATOM_HOME, 'packages', 'nvatom', 'notebook') - atom.config.set('nvatom.directory', noteDirectoryUnderPackageDirectory) - - # This is an activation event, triggering it will cause the package to be activated. - atom.commands.dispatch workspaceElement, 'nvatom:toggle' - waitsForPromise -> - activationPromise + atom.packages.activatePackage('notifications') runs -> notificationContainer = workspaceElement.querySelector('atom-notifications')