From 6412cde7a845506cb58a03dc5573bd4823634ce0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Sat, 29 Sep 2012 10:57:14 -0700 Subject: [PATCH] Support guide column callback function This allows a config function to be registered in the atom.coffee file to support different wrap guide columns depending on the type of file being viewed. --- spec/app/root-view-spec.coffee | 9 +++++-- spec/extensions/wrap-guide-spec.coffee | 26 ++++++++++++++++++++- src/app/root-view.coffee | 4 ++-- src/app/window.coffee | 4 ++-- src/extensions/wrap-guide/wrap-guide.coffee | 26 ++++++++++++++------- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index 06abe95c7..72c239418 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -406,7 +406,7 @@ describe "RootView", -> describe ".activateExtension(extension)", -> it "calls activate on the extension", -> rootView.activateExtension(extension) - expect(extension.activate).toHaveBeenCalledWith(rootView, undefined) + expect(extension.activate).toHaveBeenCalledWith(rootView, undefined, undefined) it "calls activate on the extension with its previous state", -> rootView.activateExtension(extension) @@ -414,9 +414,14 @@ describe "RootView", -> newRootView = RootView.deserialize(rootView.serialize()) newRootView.activateExtension(extension) - expect(extension.activate).toHaveBeenCalledWith(newRootView, "it worked") + expect(extension.activate).toHaveBeenCalledWith(newRootView, "it worked", undefined) newRootView.remove() + it "calls activate on the extension with the config data", -> + config = {} + rootView.activateExtension(extension, config) + expect(extension.activate).toHaveBeenCalledWith(rootView, undefined, config) + it "throws an exception if the extension has no 'name' property", -> expect(-> rootView.activateExtension({ activate: -> })).toThrow() diff --git a/spec/extensions/wrap-guide-spec.coffee b/spec/extensions/wrap-guide-spec.coffee index a8df87ec2..28d2c1649 100644 --- a/spec/extensions/wrap-guide-spec.coffee +++ b/spec/extensions/wrap-guide-spec.coffee @@ -24,7 +24,7 @@ describe "WrapGuide", -> describe "@updateGuide", -> it "positions the guide at the configured column", -> - width = editor.charWidth * wrapGuide.column + width = editor.charWidth * wrapGuide.getGuideColumn() expect(width).toBeGreaterThan(0) expect(wrapGuide.position().left).toBe(width) @@ -34,3 +34,27 @@ describe "WrapGuide", -> expect(initial).toBeGreaterThan(0) rootView.trigger('increase-font-size') expect(wrapGuide.position().left).toBeGreaterThan(initial) + + describe "overriding getGuideColumn", -> + it "invokes the callback with the editor path", -> + editorPath = null + wrapGuide.getGuideColumn = (path) -> + editorPath = path + 80 + wrapGuide.updateGuide(editor) + expect(editorPath).toBe(require.resolve('fixtures/sample.js')) + + it "uses the function from the config data", -> + rootView.find('.wrap-guide').remove() + config = + getGuideColumn: -> + 1 + requireExtension('wrap-guide', config) + wrapGuide = rootView.find('.wrap-guide').view() + expect(wrapGuide.getGuideColumn).toBe(config.getGuideColumn) + + it "hides the guide when the column is less than 1", -> + wrapGuide.getGuideColumn = (path) -> + -1 + wrapGuide.updateGuide(editor) + expect(wrapGuide).toBeHidden() diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index b0fe94482..282af030d 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -97,10 +97,10 @@ class RootView extends View when 'PaneColumn' then PaneColumn.deserialize(viewState, this) when 'Editor' then Editor.deserialize(viewState, this) - activateExtension: (extension) -> + activateExtension: (extension, config) -> throw new Error("Trying to activate an extension with no name") unless extension.name? @extensions[extension.name] = extension - extension.activate(this, @extensionStates[extension.name]) + extension.activate(this, @extensionStates[extension.name], config) deactivateExtension: (extension) -> extension.deactivate?() diff --git a/src/app/window.coffee b/src/app/window.coffee index 48d432948..4d488234e 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -61,9 +61,9 @@ windowAdditions = unless $("head style[id='#{id}']").length $('head').append "" - requireExtension: (name) -> + requireExtension: (name, config) -> extensionPath = require.resolve name - extension = rootView.activateExtension require(extensionPath) + extension = rootView.activateExtension(require(extensionPath), config) extensionKeymapPath = fs.join(fs.directory(extensionPath), "keymap.coffee") require extensionKeymapPath if fs.exists(extensionKeymapPath) diff --git a/src/extensions/wrap-guide/wrap-guide.coffee b/src/extensions/wrap-guide/wrap-guide.coffee index a4f6acbfd..c21b99efa 100644 --- a/src/extensions/wrap-guide/wrap-guide.coffee +++ b/src/extensions/wrap-guide/wrap-guide.coffee @@ -2,28 +2,38 @@ module.exports = class WrapGuide extends View - @activate: (rootView) -> + @activate: (rootView, state, config) -> requireStylesheet 'wrap-guide.css' for editor in rootView.getEditors() - @appendToEditorPane(rootView, editor) if rootView.parents('html').length + if rootView.parents('html').length + @appendToEditorPane(rootView, editor, config) rootView.on 'editor-open', (e, editor) => - @appendToEditorPane(rootView, editor) + @appendToEditorPane(rootView, editor, config) - @appendToEditorPane: (rootView, editor) -> + @appendToEditorPane: (rootView, editor, config) -> if lines = editor.pane()?.find('.lines') - lines.append(new WrapGuide(rootView, editor)) + lines.append(new WrapGuide(rootView, editor, config)) @content: -> @div class: 'wrap-guide' - column: 80 + getGuideColumn: null + + initialize: (@rootView, @editor, config = {}) => + if typeof config.getGuideColumn is 'function' + @getGuideColumn = config.getGuideColumn + else + @getGuideColumn = -> 80 - initialize: (@rootView, @editor) => @updateGuide(@editor) @editor.on 'editor-path-change', => @updateGuide(@editor) @rootView.on 'font-size-change', => @updateGuide(@editor) updateGuide: (editor) -> - @css('left', "#{editor.charWidth * @column}px") + column = @getGuideColumn(editor.getPath()) + if column > 0 + @css('left', "#{editor.charWidth * column}px").show() + else + @hide()