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.
This commit is contained in:
Kevin Sawicki
2012-09-29 10:57:14 -07:00
parent 755f1fc273
commit 6412cde7a8
5 changed files with 54 additions and 15 deletions

View File

@@ -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()

View File

@@ -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()

View File

@@ -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?()

View File

@@ -61,9 +61,9 @@ windowAdditions =
unless $("head style[id='#{id}']").length
$('head').append "<style id='#{id}'>#{text}</style>"
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)

View File

@@ -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()