Better handling of alternate spell palette documentation syntax for other code languages.

This commit is contained in:
Nick Winter 2014-06-25 20:19:11 -07:00
parent 04976a7b14
commit 8c6ee66684
5 changed files with 62 additions and 14 deletions

View file

@ -29,7 +29,16 @@ PropertyDocumentationSchema = c.object {
description: {title: "Description", type: 'string', description: "Description of the property.", format: 'markdown', maxLength: 1000}
args: c.array {title: "Arguments", description: "If this property has type 'function', then provide documentation for any function arguments."}, c.FunctionArgumentSchema
owner: {title: "Owner", type: 'string', description: 'Owner of the property, like "this" or "Math".'}
example: {title: "Example", type: 'string', description: 'An optional example code block.', format: 'javascript'}
example:
oneOf: [
{title: "Example", type: 'string', description: 'An optional example code block.', format: 'javascript'}
{
type: 'object',
title: "Language Examples",
description: "Examples by code language.",
additionalProperties: {type: 'string', description: 'An example code block.', format: 'javascript'} # TODO: not JS
}
]
returns: c.object {
title: "Return Value"
description: 'Optional documentation of any return value.'

View file

@ -4,7 +4,7 @@ ul(class="nav nav-pills" + (tabbed ? ' multiple-tabs' : ''))
each slug, group in entryGroupSlugs
li(class=group == "this" || slug == "available-spells" ? "active" : "")
a(data-toggle="pill", data-target='#palette-tab-' + slug)
h4= group
h4= entryGroupNames[group]
.tab-content
each slug, group in entryGroupSlugs
div(id="palette-tab-" + slug, class="tab-pane nano" + (group == "this" || slug == defaultGroupSlug ? " active" : ""))

View file

@ -77,22 +77,49 @@ module.exports = class SpellPaletteEntryView extends View
if _.isString @doc
@doc = name: @doc, type: typeof @thang[@doc]
if options.isSnippet
@doc.type = @doc.owner = 'snippet'
@doc.type = 'snippet'
@doc.owner = 'snippets'
@doc.shortName = @doc.shorterName = @doc.title = @doc.name
else
@doc.owner ?= 'this'
suffix = ''
ownerName = @doc.ownerName = if @doc.owner isnt 'this' then @doc.owner else switch options.language
when 'python', 'lua' then 'self'
when 'coffeescript' then '@'
else 'this'
if @doc.type is 'function'
argNames = (arg.name for arg in @doc.args ? []).join(', ')
argNames = '...' if argNames.length > 6
suffix = "(#{argNames})"
@doc.shortName = "#{@doc.owner}.#{@doc.name}#{suffix};"
if @doc.owner is 'this' or options.tabbify
@doc.shorterName = "#{@doc.name}#{suffix}"
sep = {clojure: ' '}[options.language] ? ', '
argNames = (arg.name for arg in @doc.args ? []).join sep
argString = if argNames then '__ARGS__' else ''
@doc.shortName = switch options.language
when 'coffeescript' then "#{ownerName}#{if ownerName is '@' then '' else '.'}#{@doc.name}#{if argString then ' ' + argString else '()'}"
when 'python' then "#{ownerName}.#{@doc.name}(#{argString})"
when 'lua' then "#{ownerName}:#{@doc.name}(#{argString})"
when 'clojure' then "(#{@doc.name} #{ownerName}#{if argNames then ' ' + argString else ''})"
when 'io' then "#{if ownerName is 'this' then '' else ownerName + ' '}#{@doc.name}#{if argNames then '(' + argNames + ')' else ''}"
else "#{ownerName}.#{@doc.name}(#{argString});"
else
@doc.shortName = switch options.language
when 'coffeescript' then "#{ownerName}#{if ownerName is '@' then '' else '.'}#{@doc.name}"
when 'python' then "#{ownerName}.#{@doc.name}"
when 'lua' then "#{ownerName}.#{@doc.name}"
when 'clojure' then "(#{@doc.name} #{ownerName})"
when 'io' then "#{if ownerName is 'this' then '' else ownerName + ' '}#{@doc.name}"
else "#{ownerName}.#{@doc.name};"
@doc.shorterName = @doc.shortName
if @doc.type is 'function' and argString
@doc.shortName = @doc.shorterName.replace argString, argNames
@doc.shorterName = @doc.shorterName.replace argString, (if argNames.length > 6 then '...' else argNames)
if @options.language is 'javascript'
@doc.shorterName = @doc.shortName.replace ';', ''
if @doc.owner is 'this' or options.tabbify
@doc.shorterName = @doc.shorterName.replace /^this\./, ''
@doc.title = if options.shortenize then @doc.shorterName else @doc.shortName
if example = @doc.example?[options.language]
@doc.example = example
else unless _.isString @doc.example
@doc.example = null
getRenderData: ->
c = super()
c.doc = @doc

View file

@ -31,6 +31,7 @@ module.exports = class SpellPaletteView extends View
c = super()
c.entryGroups = @entryGroups
c.entryGroupSlugs = @entryGroupSlugs
c.entryGroupNames = @entryGroupNames
c.tabbed = _.size(@entryGroups) > 1
c.defaultGroupSlug = @defaultGroupSlug
c
@ -96,7 +97,7 @@ module.exports = class SpellPaletteView extends View
return 'more' if entry.doc.owner is 'this' and entry.doc.name in (propGroups.more ? [])
entry.doc.owner
@entries = _.sortBy @entries, (entry) ->
order = ['this', 'more', 'Math', 'Vector', 'snippets']
order = ['this', 'more', 'Math', 'Vector', 'String', 'Object', 'Array', 'Function', 'snippets']
index = order.indexOf groupForEntry entry
index = String.fromCharCode if index is -1 then order.length else index
index += entry.doc.name
@ -108,13 +109,18 @@ module.exports = class SpellPaletteView extends View
@entryGroups[defaultGroup] = @entries
@defaultGroupSlug = _.string.slugify defaultGroup
@entryGroupSlugs = {}
@entryGroupNames = {}
for group, entries of @entryGroups
@entryGroupSlugs[group] = _.string.slugify group
@entryGroups[group] = _.groupBy entries, (entry, i) -> Math.floor i / N_ROWS
@entryGroupSlugs[group] = _.string.slugify group
@entryGroupNames[group] = group
if thisName = {coffeescript: '@', lua: 'self', clojure: 'self'}[@options.language]
if @entryGroupNames.this
@entryGroupNames.this = thisName
null
addEntry: (doc, shortenize, tabbify, isSnippet=false) ->
new SpellPaletteEntryView doc: doc, thang: @thang, shortenize: shortenize, tabbify: tabbify, isSnippet: isSnippet
new SpellPaletteEntryView doc: doc, thang: @thang, shortenize: shortenize, tabbify: tabbify, isSnippet: isSnippet, language: @options.language
onDisableControls: (e) -> @toggleControls e, false
onEnableControls: (e) -> @toggleControls e, true
@ -140,6 +146,9 @@ module.exports = class SpellPaletteView extends View
onTomeChangedLanguage: (e) ->
@updateCodeLanguage e.language
entry.destroy() for entry in @entries
@createPalette()
@render()
onEditEditorConfig: (e) ->
@openModalView new EditorConfigModal session: @options.session

View file

@ -272,7 +272,10 @@ module.exports = class SpellView extends View
else
@ace.setValue source
@eventsSuppressed = false
@ace.resize true # hack: @ace may not have updated its text properly, so we force it to refresh
try
@ace.resize true # hack: @ace may not have updated its text properly, so we force it to refresh
catch error
console.warn "Error resizing ACE after an update:", error
# Called from CastButtonView initially and whenever the delay is changed
setAutocastDelay: (@autocastDelay) ->