LÖVE-API

repository·master·Indexed 18 days ago

https://github.com/love2d-community/love-api

A Lua-based representation of the complete LÖVE (Love2D) API documentation. Designed for use by IDE plugins, language servers, and documentation generators to provide autocompletion, type hints, and quick references. Includes a core data structure and an extra.lua wrapper for enhanced API access, relationship mapping, and advanced lookups.

Tokens
2K
Snippets
5
Records
7
Agent score
13%

What's inside love-api

  1. Understand the LÖVE-API core data structure

    master

    The core love-api provides the complete LÖVE API documentation represented as a Lua table. The base structure organizes the API into version, functions (global functions), modules (containing name, description, types, functions, and enums), types (supertypes), and callbacks.

    Each module contains its own functions and enums. Functions are defined using a variants system, allowing for different argument sets and return types for the same function name. Types can include constructors, functions, and supertypes.

    return {
        version = '11.5',
        functions = { -- e.g. love.getVersion
        },
        modules = {
            {
                name = 'modulename',
                description = 'Description.',
                types = {},
                functions = {},
                enums = {
                    {
                        name = 'EnumName',
                        constants = {
                            { name = 'constantname', description = 'Description.' }
                        }
                    }
                }
            }
        },
        types = {},
        callbacks = {}
    }
  2. Use the extra.lua wrapper for enhanced API access

    master

    The extra.lua module is a wrapper that transforms the raw love-api table into a more developer-friendly format. It is highly recommended for tools that need to traverse or query the API easily.

    Key enhancements provided by extra.lua:

    • Unified Module Access: Moves top-level functions into a module named love (e.g., love.getVersion).
    • Guaranteed Tables: Ensures optional fields like functions, types, enums, returns, arguments, constructors, and supertypes are always present as empty tables instead of nil.
    • Relationship Mapping: Adds subtypes to types, and module, function_, and variant fields to arguments/returns to allow upward traversal.
    • Metadata: Adds what (e.g., 'function', 'method', 'type'), id (unique string), fullname (e.g., 'love.graphics.draw'), and prefix (e.g., 'love.graphics.').
    • Advanced Lookups: Provides api.everything (all tables with a what field), api.allfunctions, api.byfullname, and api.byid.
    api = require('love-api.extra')(require('love-api.love_api'))
  3. Download LÖVE wiki export files

    master

    To prepare the data for the scraper, you must download specific export files from the LÖVE wiki into a folder named wiki.

    Requirements & Constraints:

    • Use the export URL format: https://www.love2d.org/wiki/Special:Export/<page_name>
    • Do not include pages for module types or the love.conf page.
    • If a page name starts with Image: or File:, you must transform the name by wrapping the prefix in parentheses (e.g., Image:Name becomes (Image):Name) to match the wiki's export requirements.

    You can generate a list of valid URLs using the provided Lua script.

    local api = require('love-api.extra')(require('love-api.love_api'))
    
    for _, v in pairs(api.allfullnames) do
        local name = v.fullname
    
        if v.what ~= 'module' and v.fullname ~= 'love.conf' then
            if name:match('Image:') then
                name = name:gsub('^Image:(.+)', '(Image):%1')
            elseif name:match('File:') then
                name = name:gsub('^File:(.+)', '(File):%1')
            end
    
            print('https://www.love2d.org/wiki/Special:Export/'..name)
        end
    end
  4. Generate and download wiki export URLs

    master

    Follow these steps to generate a list of URLs and download them using aria2c:

    1. Run the URL generation script and redirect the output to a text file:
      lua script.lua > output.txt
    2. Use aria2c to download the files into the wiki directory using the generated list:
      aria2c --dir=wiki --input-file=output.txt
    lua script.lua > output.txt
    aria2c --dir=wiki --input-file=output.txt
  5. Access API elements by Fullname or ID using extra.lua

    master

    When using the extra.lua wrapper, you can perform direct lookups for specific API components using the byfullname or byid indices. This is useful for building IDE features like autocompletion or documentation popups.

    • api.byfullname['love.graphics.draw']: Returns the function/method table for love.graphics.draw.
    • api.byid['love_module_functionName']: Returns the table using its unique identifier.
  6. Reference the enhanced extra.lua table schema

    master

    The following schema describes the enriched structure provided by extra.lua. This is the authoritative format for consumers building tools like linters, IDE plugins, or documentation generators.

    Modules: Include fullname, prefix, minidescription, and id. Functions: Include fullname, prefix, setter, getter, constructs (points to a type), and variants. Variants: Include returns, arguments, module, function_, and type_. Arguments/Returns: Include type, name, default, module, function_, variant, and value (for returns) or table (for arguments). Types: Include fullname, parenttype, supertypes, subtypes, and constructors. Enums: Include fullname, minidescription, and constants (which include module and enum references).

    -- Example of the enriched function structure in extra.lua
    {
        name = 'functionName',
        fullname = 'love.module.functionName',
        prefix = 'love.module.',
        description = 'Description.',
        minidescription = 'Description.',
        setter = setterTable, -- Optional
        getter = getterTable, -- Optional
        constructs = typeTable, -- Optional
        variants = {
            {
                description = 'Description.',
                returns = {},
                arguments = {},
                what = 'variant',
                id = 'love_module_functionName_rb_ast',
                module = moduleTable,
                function_ = functionTable,
                type_ = typeTable,
            }
        },
        what = 'function', -- 'function', 'method', or 'callback'
        id = 'love_module_functionName',
        module = moduleTable,
        type_ = typeTable,
    }