From 91a3647a88ba16f40fcfd1d546806b6ef6d022f8 Mon Sep 17 00:00:00 2001 From: Mathijs van Velde Date: Tue, 14 Jun 2016 16:18:31 +0200 Subject: [PATCH] Update less-autocompile-view.coffee Autocompile on autosave --- lib/less-autocompile-view.coffee | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/less-autocompile-view.coffee b/lib/less-autocompile-view.coffee index fdb99c7..1d4bc6d 100644 --- a/lib/less-autocompile-view.coffee +++ b/lib/less-autocompile-view.coffee @@ -5,20 +5,35 @@ mkdirp = require 'mkdirp' path = require 'path' readline = require 'readline' +{CompositeDisposable} = require 'atom' + +LessGrammar = atom.grammars.grammarForScopeName 'source.css.less' + module.exports = class LessAutocompileView constructor: (serializeState) -> - atom.commands.add 'atom-workspace', 'core:save': => @handleSave() + disposables = new CompositeDisposable() + disposables.add atom.workspace.observeTextEditors (textEditor) => + if textEditor.getGrammar() != LessGrammar + return + textEditorDisposable = new CompositeDisposable( + textEditor.onDidSave => + @handleSave textEditor + textEditor.onDidDestroy -> + textEditorDisposable.dispose() + disposables.remove textEditorDisposable + return + ) + disposables.add textEditorDisposable + return serialize: -> destroy: -> - handleSave: -> - @activeEditor = atom.workspace.getActiveTextEditor() - - if @activeEditor - @filePath = @activeEditor.getURI() + handleSave: (@textEditor = atom.workspace.getActiveTextEditor()) -> + if @textEditor + @filePath = @textEditor.getURI() @fileExt = path.extname @filePath if @fileExt == '.less'