gittech. site

for different kinds of informations and explorations.

Sourcecrop – cat functions out of source with autocomplete

Published at
2 days ago

sourcecrop in action

Sourcecrop (aka Spirit Catcher)

sourcecrop is a command-line utility that displays code snippets (like functions, classes, or methods) from source files. It supports quick lookups, partial code extraction, and basic call analysis, making it easy to see what calls what in your JavaScript code. Other languages are on our roadmap.

Installation

Via NPM:

$ npm i -g sourcecrop@latest

Or using git:

# Clone the repository
$ git clone https://github.com/your-user/sourcecrop.git

# Navigate into the project directory
$ cd sourcecrop

# Run install script (might need sudo depending on your environment)
$ ./install.sh

This should set up a symbolic link for sc so you can use it anywhere.

Usage

$ sc <file> [name|calls-<name>|by-<name>|-listCallees|-listCallers]
  • sc : Lists all declared functions and methods in <file>, suitable for autocompletion.
  • sc someFunction: Shows the code for someFunction.
  • sc calls-someFunction: Lists all functions that call someFunction.
  • sc by-someFunction: Lists all functions called by someFunction.
  • sc -listCallees: Lists all functions called in <file>.
  • sc -listCallers: Lists all functions that call something else in <file>.

Examples:

  1. List all declared functions:

    $ sc app.js
    myFunc
    anotherFunc
    untitled-161
    
  2. Show code of a named function:

    $ sc app.js myFunc
    // Displays the source code for the function named myFunc
    
  3. Find callers of a function:

    $ sc app.js calls-myFunc
    // Lists all functions that call myFunc
    
  4. Find callees used by a specific function:

    $ sc app.js by-myFunc
    // Lists all functions that myFunc calls
    
  5. List all function calls:

    $ sc app.js -listCallees
    // Displays all distinct function calls found in the file
    

Shell Autocomplete

For easier usage, sc supports basic autocomplete when used in a bash-compatible shell. After installing, type:

$ sc app.js <TAB>

This should autocomplete declared function names or any recognized command like calls- and by-.

Roadmap

We currently focus on JavaScript (ES modules). Support for additional languages is planned.

License

sourcecrop is licensed under the GNU Affero General Public License version 3, consistent with the copyright statement in the source.

  Β© 2024 Cris (@o0101) and Dosyago Corp.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.