Glavin001/atom-beautify

Atom Beautify internal API v1.0

Glavin001 opened this issue · 8 comments

Redesigning the internals of Atom Beautify for performance, extensibility, and ease of use.

  • Base Beautifier Class
    • For both CLI beautifiers and Node.js beautifiers
    • Fields
      • Supported Grammars
      • Supported file extensions (See #238)
      • Supported options
        • Hardcoded CLI paths (see #164 ) - CLI beautifiers only
    • Beautifier class methods - to be used by Beautifier registry for filtering, etc
      • filter(editor, options)
      • Returns boolean or Error, true if this beautifier is applicable
      • Can be used to detecting if CLI path is set, and if not, the beautifier is effectively unavailable
      • Errors can be returned instead of boolean to indicate that something requires to be installed or setup, such as setting the option for the CLI path
    • API - to be used by beautifiers internally
      • getText()
        • Get text / source code to beautify
      • getOptions(language)
        • Get beautifier options for a given language / key
      • tempFile(name)
        • Create a temporary file. Used for CLI beautifiers to create input and output files to use
    • run(command)
      • Run a CLI command as a shell command
    • lazy-require(package-name)
      • See https://www.npmjs.com/package/lazy-require
      • For performance improvements, packages are lazy-loaded (loaded at usage and then cached for future usage, instead of being loaded at top of file)
      • Only packages that are used will be installed. For instance, a user may not intend to beautify JavaScript and therefore js-beautify would not be installed, unless they did run beautify on a JavaScript file
    • deprecated(warning)
      • Notify the user that something is deprecated. Could be anything from a particular option or how it was used (was string, now boolean, etc) or even that this beautifier itself is deprecated (see #275 )
  • Beautifier Registry
    • register(beautifier)
      • Beautifiers must register to be available
    • constructor
      • beautifiers are loaded from requireing beautifierNames
    • getBeautifier(editor, options)
      • Return list of applicable beautifiers based on text, grammar, file extension and options provided
      • Use filter method on Beautifier class
    • Support for multiple applicable beautifiers (see #275)
      • If multiple registers are available for this particular file then display them as a list
        • Note that beautifiers that have not set their CLI path option will not be applicable. Therefore if there are multiple applicable CLI beautifiers, only the one that has been setup (CLI path option set) will be returned as available
      • Select which beautifier used for each language in the package settings
      • Remember choice of beautifier given contents (text, grammar, file extension, options)

@Botv0091 had a great idea with #286: extract the settings from common Atom Linter packages and use them as beautification settings.

The developer API for Atom Beautify could be a package_name to function map and the function would be given the package name, settings of package, and a callback (will these be async? maybe just have it return a promise).

Look into using the object and enum type for Config API: https://atom.io/docs/api/v0.194.0/Config

Supported languages should be separated into their own list and then referenced from Beautifiers. A supported Language should have the following:

  • grammars (Atom grammar, such as JavaScript or CoffeeScript, etc)
  • extensions (file extension, such as js or coffee, etc)
  • shortname (shortname for JavaScript is js, etc; used for prefixing the options)
  • options (list of beautification options)

Tests are passing! See https://travis-ci.org/Glavin001/atom-beautify/builds/60724610
Almost ready for releasing!

Try something like:

  languages:
      type: 'object'
      properties:
          js_disabled:
              type: 'boolean'
              default: false
              description: 'Disable JavaScript Language beautification'
          js_default_beautifier:
              type: 'string'
              default: 'JS Beautifier'
              enum: ['JS Beautifier', 'Pretty Diff']