Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] autocomplete-plus support #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions lib/autocomplete.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
fuzz = require('fuzzaldrin-plus')

module.exports =
class AutoCompleter
constructor: (repl) ->
@repl = repl
@classes = null
@docs = null
# pre-allocate so it only gets compiled once
# look for some set of period-separated identifiers before the cursor
@prefixRegex = /([A-Za-z0-9_.]*)$/

handleNakedPrefix: (prefix) ->
# currently we onyly have classes in our naked database. If we
# add global variables we'll need to tweak this
names = fuzz.filter(@classes, prefix, {maxResults: 20})
return ({
text: name,
description: @docs["Classes/#{name}"],
type: "class"} for name in names)

handleClassMemberPrefix: (className, prefix) ->
candidates = []
return @getClassMethods(className)
.then (methods) =>
for m in methods
candidates.push({
text: m.name,
description: @makeMethodString(m),
type: "method"
})
return candidates

makeMethodString: (method) ->
argStrings = []
sep = ", "
N = method.args.length
if N == 0
# no arguments, show without parens
return method.name
for i in [0..(N-1)]
if method.argDefaults[i] == null
argStrings.push(method.args[i])
else
argStrings.push("#{method.args[i]}=#{method.argDefaults[i]}")
return "#{method.name}(#{argStrings.join(sep)})"

# these introspection commands return nil instead of an empty list
# if there aren't any methods/vars. bummer.
getClassMethods: (className) ->
code = """
if(#{className}.class.methods.isNil, {[]}, {
#{className}.class.methods.collect {
arg m;
(
\\name: m.name,
\\args: if(m.argNames.isNil, {[]}, {m.argNames[1..]}),
\\argDefaults: if(m.prototypeFrame.isNil, {[]}, {m.prototypeFrame[1..]})
)
}
})
"""
return @repl.eval(code, true, null, false, false)

getClassVars: (className) ->
code = """
if(#{className}.classVarNames.isNil, {[]}, {
#{className}.classVarNames
})
"""
return @repl.eval(code, true, null, false, false)

updateClassList: () ->
@repl.eval('Class.allClasses', true, null, false, false)
.then (result) =>
@classes = result
@repl.eval('SCDoc.documents.collect {arg doc; doc.summary}',
true, null, false, false)
.then (result) =>
@docs = result

getSuggestions: ({editor, bufferPosition}) =>
# Get the text for the line up to the triggered buffer position
line = editor.getTextInRange([[bufferPosition.row, 0], bufferPosition])
# this regex matches empty, so we should always get something
prefix = line.match(@prefixRegex)[0]
objs = prefix.split('.');
if objs.length == 1
return @handleNakedPrefix(objs[0])
else if objs.length == 2 && objs[0] in @classes
return @handleClassMemberPrefix(objs[0], objs[1])
else
return []
8 changes: 8 additions & 0 deletions lib/controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Controller
@activeRepl = null
@markers = []
@scScope = 'source.supercollider'
@provider =
selector: '.source.supercollider'
inclusionPriority: 10
excludeLowerPriority: true
filterSuggestions: true
getSuggestions: (options) =>
@activeRepl.getSuggestions(options)


start: ->
atom.commands.add 'atom-workspace',
Expand Down
16 changes: 13 additions & 3 deletions lib/repl.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PostWindow = require('./post-window')
AutoCompleter = require('./autocomplete')
Bacon = require('baconjs')
url = require('url')
os = require('os')
Expand All @@ -23,6 +24,7 @@ class Repl
@makeBus()
@state = null
@debug = atom.config.get 'supercollider.debug'
@autocompleter = new AutoCompleter(@)
if @debug
console.log 'Supercollider REPL [DEBUG=true]'

Expand All @@ -38,6 +40,9 @@ class Repl

@postWindow = new PostWindow(@uri, @postBus, onClose)

getSuggestions: (options) ->
@autocompleter.getSuggestions(options)

makeBus: ->
@postBus = new Bacon.Bus()
@controllerBus = new Bacon.Bus()
Expand Down Expand Up @@ -80,6 +85,7 @@ class Repl
if @debug
console.log 'booted'
@ready.resolve()
@autocompleter.updateClassList()

fail = (error) =>
# dirs
Expand Down Expand Up @@ -198,12 +204,16 @@ class Repl

return opts

eval: (expression, noecho=false, nowExecutingPath=null) ->
eval: (expression, noecho=false, nowExecutingPath=null,
returnString=true, printResult=true) ->

deferred = Q.defer()

ok = (result) =>
@postBus.push "<div class='pre out'>#{result}</div>"
if printResult
# we need to convert to string for printing if our output is a JSON object
printable = if returnString then result else JSON.stringify(result)
@postBus.push "<div class='pre out'>#{printable}</div>"
deferred.resolve(result)

err = (error) =>
Expand All @@ -223,7 +233,7 @@ class Repl
@postBus.push "<div class='pre in'>#{echo}</div>"

# expression path asString postErrors getBacktrace
@sclang.interpret(expression, nowExecutingPath, true, false, true)
@sclang.interpret(expression, nowExecutingPath, returnString, false, true)
.then(ok, err)

deferred.promise
Expand Down
3 changes: 3 additions & 0 deletions lib/supercollider.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ module.exports =

serialize: ->
{}

provide: () ->
@controller.provider
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"atom-space-pen-views": "^2.2.0",
"baconjs": "^0.7.89",
"escape-html": "~1.0.3",
"fuzzaldrin-plus": "^0.5.0",
"growl": "^1.9.2",
"jquery": "^2",
"moment": "^2.17.1",
Expand All @@ -31,5 +32,12 @@
"supercollider:cmd-period",
"supercollider:manage-quarks"
]
},
"providedServices": {
"autocomplete.provider": {
"versions": {
"2.0.0": "provide"
}
}
}
}