capital-G / sc_kernel

SuperCollider Kernel for Jupyter
BSD 3-Clause "New" or "Revised" License
20 stars 0 forks source link

Implement auto-completion #3

Closed capital-G closed 1 year ago

capital-G commented 4 years ago

This seems to be a big task but it is definitely worth it.

Maybe SC autocomplete could be implemented via https://langserver.org/

Other implementations of an IDE such as vim, emacs or atom mostly rely on static keywords.

There is an interesting idea mentioned at https://github.com/crucialfelix/atom-supercollider/issues/22#issue-41791462

capital-G commented 4 years ago

The Class documentation can be helpful for this.

List all Classes

Class.allClasses.do({|c|
    c.postln;
    nil;
});

Find all methods of a class

Array.dumpAllMethods;

returns

Instance Methods for Array

   reverse (  )
   scramble (  )
   mirror (  )
   mirror1 (  )
   mirror2 (  )
   stutter ( n )
   rotate ( n )

This also returns all Methods of all Superclasses.

Find help for class

Array.helpFilePath

returns

SCDoc: Indexing help-files...
SCDoc: Indexed 2208 documents in 1.18 seconds
-> file:///Users/xxx/Library/Application Support/SuperCollider/Help/Classes/Array.html

This html file contains a link at the end to the schelp file which can be displayed nicer than HTML in a tooltip

<div class='doclink'>helpfile source: <a href='file:///Applications/SuperCollider.app/Contents/Resources/HelpSource/Classes/Class.schelp'>/Applications/SuperCollider.app/Contents/Resources/HelpSource/Classes/Class.schelp</a><br>link::Classes/Class::<br></div></div><script src='./../editor.js' type='text/javascript'></script>
</body></html>
capital-G commented 4 years ago

A basic auto-complete is implemented but it is yet very buggy and has no intelligence in it - but regarding the reference implementation in SC IDE it seems there is no proper way to get the methods of an object.

capital-G commented 3 years ago

Taking a look at these resources could be helpful

https://github.com/supercollider/supercollider/blob/79c9d77f3946a3caf49fc0522319b52acd51878d/SCClassLibrary/scide_scqt/ScIDE.sc

https://github.com/supercollider/supercollider/blob/fb89a8b6eda01a12593b0976dadc73936628dd0c/SCClassLibrary/Common/Core/Kernel.sc#L622

capital-G commented 1 year ago
(
var methodExtractor = {|method|
    (
        name: method.name,
        arguments: method.argNames.collect({|argName, i|
            (
                name: argName,
                default: method.prototypeFrame[i],
            )
        });
    )
};

x = Class.allClasses.collect({|class|   
    (
        name: class.name,
        sourceFile: class.filenameSymbol,
                // meta class does not have helpFilePath implemented
        helpFile: if(class.isMetaClass, {""}, {class.helpFilePath}),
        methods: class.methods.collect({|m|
            methodExtractor.value(m);
        }),
        classMethods: class.class.methods.collect({|m|
            methodExtractor.value(m);
        });
    )
});
)

one class as JSON becomes

{
    "classMethods": [
        {
            "arguments": [
                {
                    "name": "this"
                },
                {
                    "default": 28,
                    "name": "columns"
                },
                {
                    "default": 7,
                    "name": "rows"
                },
                {
                    "default": 0.6,
                    "name": "lambda"
                },
                {
                    "default": 1.0,
                    "name": "sigma"
                },
                {
                    "default": 2,
                    "name": "singleEventsPerSec"
                },
                {
                    "default": 5,
                    "name": "maxEventOrder"
                }
            ],
            "name": "new"
        },
        {
            "arguments": [
                {
                    "name": "this"
                },
                {
                    "name": "lambda"
                },
                {
                    "name": "k"
                }
            ],
            "name": "poisson"
        }
    ],
    "helpFile": "file:\/\/\/Users\/scheiba\/Library\/Application Support\/SuperCollider\/Help\/Classes\/Aachorripsis.html",
    "methods": [
        {
            "name": "columns"
        },
        {
            "name": "rows"
        },
        {
            "name": "lambda"
        },
        {
            "name": "sigma"
        },
        {
            "name": "maxEventOrder"
        },
        {
            "name": "matrix"
        },
        {
            "name": "p"
        },
        {
            "arguments": [
                {
                    "name": "this"
                }
            ],
            "name": "init"
        },
        {
            "arguments": [
                {
                    "name": "this"
                },
                {
                    "name": "eventType"
                },
                {
                    "name": "simEvents"
                }
            ],
            "name": "prInsertEvent"
        }
    ],
    "name": "Aachorripsis",
    "sourceFile": "\/Users\/scheiba\/github\/aachorripsis\/Classes\/Aachorripsis.sc"
}

Maybe we could parse the Help Files like in the IDE as well?

image
capital-G commented 1 year ago

this results in a 13mb JSON which is not really practical anymore :/ Maybe we can instead rely on the OSC communication for this?

capital-G commented 1 year ago

More ressources

https://github.com/supercollider/supercollider/blob/18c4aad363c49f29e866f884f5ac5bd35969d828/editors/sc-ide/core/sc_introspection.cpp

https://github.com/supercollider/supercollider/blob/18c4aad363c49f29e866f884f5ac5bd35969d828/editors/sc-ide/widgets/code_editor/autocompleter.cpp

capital-G commented 1 year ago

More https://github.com/supercollider/supercollider/blob/18c4aad363c49f29e866f884f5ac5bd35969d828/editors/sc-ide/widgets/code_editor/tokens.hpp