Skip to content

Commit

Permalink
feat: add config for sources
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Aug 1, 2024
1 parent d83528c commit a764c34
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
25 changes: 20 additions & 5 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
title: Source
description: Type description of care.nvim config
author:
author:
- max397574
categories:
categories:
- docs,
- types
tangle:
languages:
tangle:
languages:
lua: ../lua/care/types/config.lua
scope: tagged
delimiter: none
Expand All @@ -16,40 +16,51 @@ updated: 2024-07-14T09:46:26+0100
version: 1.1.1
---


# General

The config of care is used to configure the ui and care itself.

There are two main parts to the config. The first one is the `ui` field and the second on is the
rest of the configuration which is for configuring care itself.

## UI

In the ui field the completion menu, the docs view and the format of the entries are configured.
There is also a field for configuring type icons.

## Snippet expansion

Here a function for expanding snippets is defined. By default this is the builtin
`vim.snippet.expand()`. You can also use a plugin like luasnip for this like this:

```lua
snippet_expansion = function(body)
require("luasnip").lsp_expand(body)
end
```

## Selection behavior

With the selection behavior the user can determine what happens when selecting an entry. This can
either be `select` or `insert`. Selecting will just select the entry and do nothing else. Insert
will actually insert the text of the entry (this is not necessarily the whole text).

## Keyword pattern

Pattern used to determine keywords, used to determine what to use for filtering and what to
remove if insert text is used.

## Sources

TODO

## Enabled

This function can be used to disable care in certain contexts. By default this disables
care in prompts.

# UI

The ui configuration is used to configure the whole ui of care. One of the main goals of
this is to be as extensible as possible. This is especially important for the completion entries.
Read more about that under [Configuraton of item display](./design.md#configuraton-of-item-display).
Expand All @@ -72,6 +83,7 @@ where the cursor is. With the overlay position the text will overlap with existi
cursor.

## Menu

This configuration should allow you to completely adapt the completion menu to your likings.

It includes some basic window properties like the border and the maximum height of the window. It
Expand All @@ -91,6 +103,7 @@ with the alignment of each section.
For example you want to have the label of an entry in a red highlight and an icon in a entry-kind
specific color left aligned first and then the source of the entry right aligned in blue.
You could do that like this:

```lua
format_entry = function(entry)
return {
Expand All @@ -110,9 +123,11 @@ aligned at the same column and not be next to the labels. In the example there a
spacing added in between the two.

## Documentation view

This configuration allows you to configure the documentation view.
It consists of some basic window properties like the border and the maximum height of the window.
It also has a field to define the character used for the scrollbar.

## Type Icons

This is a table which defines the different icons.
26 changes: 21 additions & 5 deletions docs/source.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,81 @@
---
title: Source
description: Type description of care.nvim source
author:
author:
- max397574
categories:
categories:
- docs,
- types
created: 2023-11-15T17:42:46+0100
updated: 2024-07-15T20:10:47+0100
tangle:
languages:
tangle:
languages:
lua: ../lua/care/types/source.lua
scope: tagged
delimiter: none
version: 1.1.1
---


# General

The sources are used to get get completions for care.nvim.

# Fields

## Name

There are two fields for the name of a source. They are both strings. The `name` field is used for
configuring the source. It should just contain characters, `_`, and `-`. There is also the
`display_name` field. This name is displayed in sources overview. It can be any string.

The display name is optional and falls back to the normal name.

# Methods

## `is_available()`

Each source can have a function to show whether it's available or not. If your source should
for example be enabled for a certain filetype you can just do it like this:

```lua
function my_source.is_available()
return vim.bo.ft == "lua"
end
```

This function will be called quite often so developers should try to keep it more or less
performant. This won't be an issue in the vast majority of cases though.

## Resolve

This is a function used to get additional details for completion items. This is especially
important for the lsp source which needs to send the `completionItem/resolve` request.
Resolving completion items is used for performance reasons so e.g. the documentation for an item
doesn't always have to be sent.

## `get_trigger_characters()`

This function should return characters which trigger completion for the source. If one of those
characters is types the completion will be retriggered. Otherwise newly entered characters are
used for sorting and filtering.
An example for this could be `.`, `\\` and `/` when working with paths.

```lua
function my_source.get_trigger_characters()
return { ".", "\\", "/" }
end
```

## Keyword pattern

The keyword pattern is used to overwrite the keyword pattern from the config per source. It
should basically represent the format of entries the source will provide as regex.
It can either be provided as a string with `keyword_pattern` or dynamically with
`get_keyword_pattern`.
The `get_keyword_pattern` function has higher priority and will overwrite the string if provided.

## Complete

This is arguably the most important function of each source. This function returns completions.
The function takes in a [completion context](./index.md#completion-context) and should return a
list of [entries](#entrymd).
1 change: 1 addition & 0 deletions lua/care/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ config.defaults = {
end,
selection_behavior = "select",
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
sources = {},
enabled = function()
local enabled = true
if vim.api.nvim_get_option_value("buftype", { buf = 0 }) == "prompt" then
Expand Down
1 change: 1 addition & 0 deletions lua/care/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function core:complete(reason)
self.context.reason = reason
local offset = self.context.cursor.col
for i, source in ipairs(sources) do
-- TODO: check if enabled in config
if source.source.is_available() then
require("care.sources").complete(self.context, source, function(items, is_incomplete)
source.incomplete = is_incomplete or false
Expand Down
10 changes: 10 additions & 0 deletions lua/care/types/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
---@field type_icons care.config.ui.type_icons
--- Configuration of ghost text
---@field ghost_text care.config.ui.ghost_text
--- Configuration for the different sources
---@field sources neocomplete.config.source[]

--- Configuration for the ghost text
---@class care.config.ui.ghost_text
Expand All @@ -41,6 +43,14 @@
--- How the sections in the menu should be aligned
---@field alignment ("left"|"center"|"right")[]

---@class neocomplete.config.source
--- Whether the source is enabled (default true)
---@field enabled (fun(): boolean)?|boolean?
--- The maximum amount of entries which can be displayed by this source
---@field max_entries integer?
--- The priority of this source. Is more important than matching score
---@field priority integer?

--- Configuration of the completion menu of care.nvim
---@class care.config.ui.docs
--- Maximum height of the documentation view
Expand Down
4 changes: 3 additions & 1 deletion lua/care/types/source.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
--- A completion source for care.nvim
---@class care.source
--- Name of the source
--- Name of the source (used for configuration)
---@field name string
--- Display name of the source (used for display)
---@field display_name string?
--- Whether the source will provide completions in the current context or not
---@field is_available? fun(): boolean
--- Resolve a completion item
Expand Down

0 comments on commit a764c34

Please sign in to comment.