Skip to content

Latest commit

 

History

History
1115 lines (625 loc) · 22 KB

api.md

File metadata and controls

1115 lines (625 loc) · 22 KB

Lua API

astrocore API documentation

astrocore

cmd

function astrocore.cmd(cmd: string|string[], show_error?: boolean)
  -> string|nil

Run a shell command and capture the output and if the command succeeded or failed

param cmd — The terminal command to execute

param show_error — Whether or not to show an unsuccessful command as an error to the user

return — The result of a successfully executed command or nil

conditional_func

function astrocore.conditional_func(func: function, condition: boolean, ...any)
  -> result: any

Call function if a condition is met

param func — The function to run

param condition — Whether to run the function or not

return result — the result of the function running or nil

config

AstroCoreOpts

The configuration as set by the user through the setup() function

delete_url_match

function astrocore.delete_url_match(win?: integer)

Delete the syntax matching rules for URLs/URIs if set

param win — the window id to remove url highlighting in (default: current window)

diagnostics

{ [integer]: vim.diagnostic.Opts }

A table of settings for different levels of diagnostics

empty_map_table

function astrocore.empty_map_table()
  -> table<string, table>

Get an empty table of mappings with a key for each map mode

return — a table with entries for each map mode

event

function astrocore.event(event: string|vim.api.keyset_exec_autocmds, instant?: boolean)

Trigger an AstroNvim user event

param event — The event pattern or full autocmd options (pattern always prepended with "Astro")

param instant — Whether or not to execute instantly or schedule

exec_buffer_autocmds

function astrocore.exec_buffer_autocmds(event: string|string[], opts: vim.api.keyset.exec_autocmds)

Execute autocommand across all valid buffers

param event — the event or events to execute

param opts — Dictionary of autocommnd options

extend_tbl

function astrocore.extend_tbl(default?: table, opts?: table)
  -> table

Merge extended options with a default table of options

param default — The default table that you want to merge into

param opts — The new options that should be merged with the default table

return — The merged table

file_worktree

function astrocore.file_worktree(file?: string, worktrees?: table<string, string>[])
  -> table<string, string>|nil

Get the first worktree that a file belongs to

param file — the file to check, defaults to the current file

param worktrees — an array like table of worktrees with entries toplevel and gitdir, default retrieves from vim.g.git_worktrees

return — a table specifying the toplevel and gitdir of a worktree or nil if not found

get_plugin

function astrocore.get_plugin(plugin: string)
  -> available: LazyPlugin?

Get a plugin spec from lazy

param plugin — The plugin to search for

return available — The found plugin spec from Lazy

is_available

function astrocore.is_available(plugin: string)
  -> available: boolean

Check if a plugin is defined in lazy. Useful with lazy loading when a plugin is not necessarily loaded yet

param plugin — The plugin to search for

return available — Whether the plugin is available

list_insert_unique

function astrocore.list_insert_unique(dst: any[]|nil, src: any[])
  -> any[]

Insert one or more values into a list like table and maintain that you do not insert non-unique values (THIS MODIFIES dst)

param dst — The list like table that you want to insert into

param src — Values to be inserted

return — The modified list like table

load_plugin_with_func

function astrocore.load_plugin_with_func(plugin: string, module: table, funcs: string|string[])

A helper function to wrap a module function to require a plugin before running

param plugin — The plugin to call require("lazy").load with

param module — The system module where the functions live (e.g. vim.ui)

param funcs — The functions to wrap in the given module (e.g. "ui", "select")

notify

function astrocore.notify(msg: string, type: integer|nil, opts?: table)

Serve a notification with a title of AstroNvim

param msg — The notification body

param type — The type of the notification (:help vim.log.levels)

param opts — The nvim-notify options to use (:help notify-options)

on_load

function astrocore.on_load(plugins: string|string[], load_op: string|fun()|string[])

Execute a function when a specified plugin is loaded with Lazy.nvim, or immediately if already loaded

param plugins — the name of the plugin or a list of plugins to defer the function execution on. If a list is provided, only one needs to be loaded to execute the provided function

param load_op — the function to execute when the plugin is loaded, a plugin name to load, or a list of plugin names to load

patch_func

function astrocore.patch_func(orig?: function, override: fun(orig: function, ...any):...unknown)
  -> the: function

Monkey patch into an existing function

Example from :h vim.paste()

local patch_func = require("astrocore").patch_func
vim.paste = patch_func(vim.paste, function(orig, lines, phase)
  for i, line in ipairs(lines) do
    -- Scrub ANSI color codes from paste input.
    lines[i] = line:gsub('\27%[[0-9;mK]+', '')
  end
  return orig(lines, phase)
end)

param orig — the original function to override, if nil is provided then an empty function is passed

param override — the override function

return the — new function with the patch applied

plugin_opts

function astrocore.plugin_opts(plugin: string)
  -> opts: table

Resolve the options table for a given plugin with lazy

param plugin — The plugin to search for

return opts — The plugin options

read_file

function astrocore.read_file(path: string)
  -> content: string

Helper function to read a file and return it's content

param path — the path to the file to read

return content — the contents of the file

reload

function astrocore.reload()

Partially reload AstroNvim user settings. Includes core vim options, mappings, and highlights. This is an experimental feature and may lead to instabilities until restart.

set_mappings

function astrocore.set_mappings(map_table: table<string, table<string, (string|function|AstroCoreMapping|false)?>?>, base?: vim.keymap.set.Opts)

Table based API for setting keybindings

param map_table — A nested table where the first key is the vim mode, the second key is the key to map, and the value is the function to set the mapping to

param base — A base set of options to set on every keybinding

set_url_match

function astrocore.set_url_match(win?: integer)

Add syntax matching rules for highlighting URLs/URIs

param win — the window id to remove url highlighting in (default: current window)

setup

function astrocore.setup(opts: AstroCoreOpts)

Setup and configure AstroCore See: astrocore.config

system_open

function astrocore.system_open(path: string)

Open a URL under the cursor with the current operating system

param path — The path of the file to open with the system opener

toggle_term_cmd

function astrocore.toggle_term_cmd(opts: string|table)

Toggle a user terminal if it exists, if not then create a new one and save it

param opts — A terminal command string or a table of options for Terminal:new() (Check toggleterm.nvim documentation for table format)

unique_list

function astrocore.unique_list(list: any[])
  -> any[]

Remove duplicate entries from a given list (does not mutate the original list)

param list — The list like table that you want to remove duplicates from

return — The list like table of unique values

update_packages

function astrocore.update_packages()

Sync Lazy and then update Mason

url_matcher

string

regex used for matching a valid URL/URI string

user_terminals

{ [string]: table<integer, table> }

A table to manage ToggleTerm terminals created by the user, indexed by the command run and then the instance number

which_key_queue

nil

A placeholder variable used to queue section names to be registered by which-key

which_key_register

function astrocore.which_key_register()

Register queued which-key mappings

with_file

function astrocore.with_file(filename: string, mode?: "a"|"a+"|"a+b"|"ab"|"r"...(+7), callback?: fun(file: file*), on_error?: fun(err: string))

Execute function with open file

param filename — path to file to interact with

param mode — the mode in which to open the file

param callback — the callback to execute with the opened file

param on_error — the callback to execute if unable to open the file

mode:
   -> "r" -- Read mode.
    | "w" -- Write mode.
    | "a" -- Append mode.
    | "r+" -- Update mode, all previous data is preserved.
    | "w+" -- Update mode, all previous data is erased.
    | "a+" -- Append update mode, previous data is preserved, writing is only allowed at the end of file.
    | "rb" -- Read mode. (in binary mode.)
    | "wb" -- Write mode. (in binary mode.)
    | "ab" -- Append mode. (in binary mode.)
    | "r+b" -- Update mode, all previous data is preserved. (in binary mode.)
    | "w+b" -- Update mode, all previous data is erased. (in binary mode.)
    | "a+b" -- Append update mode, previous data is preserved, writing is only allowed at the end of file. (in binary mode.)

astrocore.buffer

close

function astrocore.buffer.close(bufnr?: integer, force?: boolean)

Close a given buffer

param bufnr — The buffer to close or the current buffer if not provided

param force — Whether or not to foce close the buffers or confirm changes (default: false)

close_all

function astrocore.buffer.close_all(keep_current?: boolean, force?: boolean)

Close all buffers

param keep_current — Whether or not to keep the current buffer (default: false)

param force — Whether or not to foce close the buffers or confirm changes (default: false)

close_left

function astrocore.buffer.close_left(force?: boolean)

Close buffers to the left of the current buffer

param force — Whether or not to foce close the buffers or confirm changes (default: false)

close_right

function astrocore.buffer.close_right(force?: boolean)

Close buffers to the right of the current buffer

param force — Whether or not to foce close the buffers or confirm changes (default: false)

close_tab

function astrocore.buffer.close_tab(tabpage?: integer)

Close a given tab

param tabpage — The tabpage to close or the current tab if not provided

current_buf

nil

Placeholders for keeping track of most recent and previous buffer

has_filetype

function astrocore.buffer.has_filetype(bufnr?: integer)
  -> boolean

Check if a buffer has a filetype

param bufnr — The buffer to check, default to current buffer

return — Whether the buffer has a filetype or not

is_restorable

function astrocore.buffer.is_restorable(bufnr: integer)
  -> boolean

Check if a buffer can be restored

param bufnr — The buffer to check

return — Whether the buffer is restorable or not

is_valid

function astrocore.buffer.is_valid(bufnr?: integer)
  -> boolean

Check if a buffer is valid

param bufnr — The buffer to check, default to current buffer

return — Whether the buffer is valid or not

is_valid_session

function astrocore.buffer.is_valid_session()
  -> boolean

Check if the current buffers form a valid session

return — Whether the current session of buffers is a valid session

last_buf

nil

move

function astrocore.buffer.move(n: integer)

Move the current buffer tab n places in the bufferline

param n — The number of tabs to move the current buffer over by (positive = right, negative = left)

nav

function astrocore.buffer.nav(n: integer)

Navigate left and right by n places in the bufferline

param n — The number of tabs to navigate to (positive = right, negative = left)

nav_to

function astrocore.buffer.nav_to(tabnr: integer)

Navigate to a specific buffer by its position in the bufferline

param tabnr — The position of the buffer to navigate to

prev

function astrocore.buffer.prev()

Navigate to the previously used buffer

sessions

unknown

sort

function astrocore.buffer.sort(compare_func: string|function, skip_autocmd: boolean|nil)
  -> boolean

Sort a the buffers in the current tab based on some comparator

param compare_func — a string of a comparator defined in require("astrocore.buffer.comparator") or a custom comparator function

param skip_autocmd — whether or not to skip triggering AstroBufsUpdated autocmd event

return — Whether or not the buffers were sorted

wipe

function astrocore.buffer.wipe(bufnr?: integer, force?: boolean)

Fully wipeout a given buffer

param bufnr — The buffer to wipe or the current buffer if not provided

param force — Whether or not to foce close the buffers or confirm changes (default: false)

astrocore.buffer.comparator

bufnr

function astrocore.buffer.comparator.bufnr(bufnr_a: integer, bufnr_b: integer)
  -> comparison: boolean

Comparator of two buffer numbers

param bufnr_a — buffer number A

param bufnr_b — buffer number B

return comparison — true if A is sorted before B, false if B should be sorted before A

extension

function astrocore.buffer.comparator.extension(bufnr_a: integer, bufnr_b: integer)
  -> comparison: boolean

Comparator of two buffer numbers based on the file extensions

param bufnr_a — buffer number A

param bufnr_b — buffer number B

return comparison — true if A is sorted before B, false if B should be sorted before A

full_path

function astrocore.buffer.comparator.full_path(bufnr_a: integer, bufnr_b: integer)
  -> comparison: boolean

Comparator of two buffer numbers based on the full path

param bufnr_a — buffer number A

param bufnr_b — buffer number B

return comparison — true if A is sorted before B, false if B should be sorted before A

modified

function astrocore.buffer.comparator.modified(bufnr_a: integer, bufnr_b: integer)
  -> comparison: boolean

Comparator of two buffers based on modification date

param bufnr_a — buffer number A

param bufnr_b — buffer number B

return comparison — true if A is sorted before B, false if B should be sorted before A

unique_path

function astrocore.buffer.comparator.unique_path(bufnr_a: integer, bufnr_b: integer)
  -> comparison: boolean

Comparator of two buffers based on their unique path

param bufnr_a — buffer number A

param bufnr_b — buffer number B

return comparison — true if A is sorted before B, false if B should be sorted before A

astrocore.mason

update

function astrocore.mason.update(pkg_names?: string|string[], auto_install?: boolean)

Update specified mason packages, or just update the registries if no packages are listed

param pkg_names — The package names as defined in Mason (Not mason-lspconfig or mason-null-ls) if the value is nil then it will just update the registries

param auto_install — whether or not to install a package that is not currently installed (default: True)

update_all

function astrocore.mason.update_all()

Update all packages in Mason

astrocore.rooter

bufpath

function astrocore.rooter.bufpath(bufnr: integer)
  -> path: string?

Get the real path of a buffer

param bufnr — the buffer

return path — the real path

detect

function astrocore.rooter.detect(bufnr?: integer, all?: boolean, config?: AstroCoreRooterOpts)
  -> detected: AstroCoreRooterRoot[]

Detect roots in a given buffer

param bufnr — the buffer to detect

param all — whether to return all roots or just one

param config — a rooter configuration (defaults to global configuration)

return detected — roots

exists

function astrocore.rooter.exists(path: string)
  -> exists: boolean

Check if a path exists

param path — the path

return exists — whether or not the path exists

info

function astrocore.rooter.info(config?: AstroCoreRooterOpts)

Get information information about the current root

param config — a rooter configuration (defaults to global configuration)

is_excluded

function astrocore.rooter.is_excluded(path: string, config?: AstroCoreRooterOpts)
  -> excluded: boolean

Check if a path is excluded

param path — the path

param config — a rooter configuration (defaults to global configuration)

return excluded — whether or not the path is excluded

normpath

function astrocore.rooter.normpath(path: string)
  -> string

Normalize path

realpath

function astrocore.rooter.realpath(path?: string)
  -> the: string?

Resolve a given path

param path — the path to resolve

return the — resolved path

resolve

function astrocore.rooter.resolve(spec: string|fun(bufnr: integer):string|string[]|string[], config?: AstroCoreRooterOpts)
  -> function

Resolve the root detection function for a given spec

param spec — the root detector specification

param config — the root configuration

root

function astrocore.rooter.root(bufnr?: integer, config?: AstroCoreRooterOpts)

Run the root detection and set the current working directory if a new root is detected

param bufnr — the buffer to detect

param config — a rooter configuration (defaults to global configuration)

set_pwd

function astrocore.rooter.set_pwd(root: AstroCoreRooterRoot, config?: AstroCoreRooterOpts)
  -> success: boolean

Set the current directory to a given root

param root — the root to set the pwd to

param config — a rooter configuration (defaults to global configuration)

return success — whether or not the pwd was successfully set

astrocore.toggles

autochdir

function astrocore.toggles.autochdir(silent?: boolean)

Toggle rooter autochdir

param silent — if true then don't sent a notification

autopairs

function astrocore.toggles.autopairs(silent?: boolean)

Toggle autopairs

param silent — if true then don't sent a notification

background

function astrocore.toggles.background(silent?: boolean)

Toggle background="dark"|"light"

param silent — if true then don't sent a notification

buffer_cmp

function astrocore.toggles.buffer_cmp(bufnr?: integer, silent?: boolean)

Toggle buffer local cmp

param bufnr — the buffer to toggle cmp completion on

param silent — if true then don't sent a notification

buffer_syntax

function astrocore.toggles.buffer_syntax(bufnr?: integer, silent?: boolean)

Toggle syntax highlighting and treesitter

param bufnr — the buffer to toggle syntax on

param silent — if true then don't sent a notification

cmp

function astrocore.toggles.cmp(silent?: boolean)

Toggle cmp entrirely

param silent — if true then don't sent a notification

conceal

function astrocore.toggles.conceal(silent?: boolean)

Toggle conceal=2|0

param silent — if true then don't sent a notification

diagnostics

function astrocore.toggles.diagnostics(silent?: boolean)

Toggle diagnostics

param silent — if true then don't sent a notification

foldcolumn

function astrocore.toggles.foldcolumn(silent?: boolean)

Toggle foldcolumn=0|1

param silent — if true then don't sent a notification

indent

function astrocore.toggles.indent(silent?: boolean)

Set the indent and tab related numbers

param silent — if true then don't sent a notification

notifications

function astrocore.toggles.notifications(silent?: boolean)

Toggle notifications for UI toggles

param silent — if true then don't sent a notification

number

function astrocore.toggles.number(silent?: boolean)

Change the number display modes

param silent — if true then don't sent a notification

paste

function astrocore.toggles.paste(silent?: boolean)

Toggle paste

param silent — if true then don't sent a notification

signcolumn

function astrocore.toggles.signcolumn(silent?: boolean)

Toggle signcolumn="auto"|"no"

param silent — if true then don't sent a notification

spell

function astrocore.toggles.spell(silent?: boolean)

Toggle spell

param silent — if true then don't sent a notification

statusline

function astrocore.toggles.statusline(silent?: boolean)

Toggle laststatus=3|2|0

param silent — if true then don't sent a notification

tabline

function astrocore.toggles.tabline(silent?: boolean)

Toggle showtabline=2|0

param silent — if true then don't sent a notification

url_match

function astrocore.toggles.url_match(silent?: boolean)

Toggle URL/URI syntax highlighting rules

param silent — if true then don't sent a notification

wrap

function astrocore.toggles.wrap(silent?: boolean)

Toggle wrap

param silent — if true then don't sent a notification