Skip to content

Commit

Permalink
feat: filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Jun 8, 2024
1 parent 78800f1 commit 5cfed8b
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 71 deletions.
8 changes: 8 additions & 0 deletions docs/config.norg
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ version: 1.1.1
---@field snippet_expansion fun(string): nil
@end

** Keyword pattern
Pattern used to determine keywords, used to determine what to use for filtering and
#tangle
@code lua
--- Pattern used to determine keywords
---@field keyword_pattern string
@end

** Enabled
This function can be used to disable neocomplete in certain contexts. By default this disables
neocomplete in prompts.
Expand Down
13 changes: 11 additions & 2 deletions docs/context.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2023-11-15T17:42:46+0100
updated: 2024-05-28T20:54:48+0100
updated: 2024-06-07T21:22:41+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/context.lua
Expand Down Expand Up @@ -66,6 +66,16 @@ version: 1.1.1
---@field bufnr integer
@end

** Reason
Reason for triggering completion
#tangle
@code lua
--- Reason for triggering completion
---@field reason neocomplete.completionReason?
@end




#tangle
@code lua
Expand All @@ -74,4 +84,3 @@ version: 1.1.1
---@field row integer
---@field col integer
@end

4 changes: 2 additions & 2 deletions docs/core.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2023-11-15T17:42:46+0100
updated: 2024-05-29T16:24:09+0100
updated: 2024-06-07T21:25:42+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/core.lua
Expand Down Expand Up @@ -54,7 +54,7 @@ version: 1.1.1
#tangle
@code lua
--- Complete
---@field complete fun(neocomplete.core): nil
---@field complete fun(self: neocomplete.core, reason: neocomplete.completionReason?): nil
@end

** On Change
Expand Down
35 changes: 26 additions & 9 deletions docs/entry.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2023-11-15T17:42:46+0100
updated: 2024-05-31T11:33:54+0100
updated: 2024-06-08T14:15:40+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/entry.lua
Expand All @@ -28,13 +28,30 @@ version: 1.1.1
---@class neocomplete.entry: lsp.CompletionItem
@end

** Matches
Position of matches during filtering
#tangle
@code lua
--- Matches in filter text
---@field matches integer[]
@end

** Score
Score from filtering
#tangle
@code lua
--- Score from filtering
---@field score number
@end


** Kind
The optional `kind` property is used to determine the kind of the completion item. This is often
used to display an icon representing it. In neocomplete this can either be a number like in the
lsp-specification of a string.
The optional `kind` property is used to determine the kind of the completion item. This is often
used to display an icon representing it. In neocomplete this can either be a number like in the
lsp-specification of a string.

#tangle
@code lua
--- The kind of the completion item
---@field kind? string|lsp.CompletionItemKind
@end
#tangle
@code lua
--- The kind of the completion item
---@field kind? string|lsp.CompletionItemKind
@end
67 changes: 67 additions & 0 deletions docs/internal_source.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@document.meta
title: Internal source
description: Type description of internal neovim source
authors: [
max397574
]
categories: [
docs,
types
]
created: 2024-05-31T12:48:14+0100
updated: 2024-06-08T14:48:14+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/internal_source.lua
}
scope: tagged
delimiter: none
}
version: 1.1.1
@end

* General
The internal sources are used on top of {:./source:}[completion sources] to store additional
metadata about which the source author doesn't have to care.

#tangle
@code lua
--- Internal class for neocomplete to store source metadata
---@class neocomplete.internal_source
@end

** Source
#tangle
@code lua
--- The external completion source
---@field source neocomplete.source
@end


** Entries
#tangle
@code lua
--- The entries recieved by last completion
---@field entries neocomplete.entry[]
@end

** New
#tangle
@code lua
--- The entries recieved by last completion
---@field new fun(completion_source: neocomplete.source): neocomplete.internal_source
@end

** Incomplete
#tangle
@code lua
--- Whether the source is incomplete
---@field incomplete boolean
@end

** Get offset
#tangle
@code lua
--- The entries recieved by last completion
---@field get_offset fun(self: neocomplete.internal_source, context: neocomplete.context): integer
@end
7 changes: 4 additions & 3 deletions docs/menu.norg
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ version: 1.1.1
#tangle
@code lua
--- Opens window for the menu
---@field open_win fun(self: neocomplete.menu): nil
---@field open_win fun(self: neocomplete.menu, offset: integer): nil
@end

** Is open
#tangle
@code lua
--- Opens window for the menu
--- Returns true if completion window is visible
---@field is_open fun(self: neocomplete.menu): boolean
@end


** Select next
#tangle
@code lua
Expand All @@ -75,7 +76,7 @@ version: 1.1.1
#tangle
@code lua
--- Opens the menu with specified entries
---@field open fun(self: neocomplete.menu, entries: neocomplete.entry[]): nil
---@field open fun(self: neocomplete.menu, entries: neocomplete.entry[], offset: integer): nil
@end

** Close
Expand Down
10 changes: 9 additions & 1 deletion docs/misc.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2024-05-29T11:30:25+0100
updated: 2024-05-29T11:32:14+0100
updated: 2024-06-07T21:33:41+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/misc.lua
Expand All @@ -31,3 +31,11 @@ version: 1.1.1
---@field context neocomplete.context
@end

* Reason
#tangle
@code lua
--- Reason for triggering completion
---@alias neocomplete.completionReason
---| 1 # Auto
---| 2 # Manual
@end
21 changes: 17 additions & 4 deletions docs/source.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2023-11-15T17:42:46+0100
updated: 2024-05-31T11:34:53+0100
updated: 2024-06-08T14:53:02+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/source.lua
Expand All @@ -20,8 +20,6 @@ tangle: {
version: 1.1.1
@end

TODO: decide about object-orientation of sources

* General
The sources are used to get get completions for neocomplete.nvim.

Expand Down Expand Up @@ -67,6 +65,21 @@ TODO: decide about object-orientation of sources
---@field get_trigger_characters? fun(): string[]
@end

** Keyword pattern
`get_keyword_pattern` function has higher priority
#tangle
@code lua
--- Keyword pattern for this source
---@field keyword_pattern? string
@end

#tangle
@code lua
--- Get keyword pattern for this source
---@field get_keyword_pattern? fun(self: neocomplete.source): string
@end


** `complete()`
This is arguably the most important function of each source. This function returns completions.
The function takes in a {:./index:* Completion Context}[completion context] and should return a
Expand All @@ -75,5 +88,5 @@ TODO: decide about object-orientation of sources
#tangle
@code lua
--- Returns completion in the provided context
---@field complete fun(completion_context: neocomplete.completion_context, callback: fun(items: neocomplete.entry[])): nil
---@field complete fun(completion_context: neocomplete.completion_context, callback: fun(items: neocomplete.entry[], is_incomplete?: boolean)): nil
@end
1 change: 1 addition & 0 deletions lua/neocomplete/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ config.defaults = {
snippet_expansion = function(snippet_body)
vim.snippet.expand(snippet_body)
end,
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
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/neocomplete/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local context = {}
---@param previous neocomplete.context?
---@return neocomplete.context
function context.new(previous)
---@type neocomplete.context
local self = setmetatable({}, { __index = context })
previous = previous or {}
previous.previous = nil
Expand Down
24 changes: 17 additions & 7 deletions lua/neocomplete/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@ function core.new()
return self
end

function core:complete()
function core:complete(reason)
reason = reason or 2
local sources = require("neocomplete.sources").get_sources()
local entries = {}
local remaining = #sources
for _, source in ipairs(sources) do
if source.is_available() then
require("neocomplete.sources").complete(self.context, source, function(items)
self.context.reason = reason
local offset = 0
for i, source in ipairs(sources) do
if source.source.is_available() then
require("neocomplete.sources").complete(self.context, source, function(items, is_incomplete)
source.incomplete = is_incomplete or false
source.entries = items
require("neocomplete.sources").sources[i].incomplete = is_incomplete or false
require("neocomplete.sources").sources[i].entries = items
remaining = remaining - 1
offset = math.max(offset, source:get_offset(self.context))
if items and not vim.tbl_isempty(items) then
vim.list_extend(entries, items)
vim.schedule(function()
if remaining == 0 then
self.menu:open(entries)
-- TODO: source priority and max entries
self.menu:open(entries, offset)
end
end)
end
Expand All @@ -43,10 +52,11 @@ function core.setup(self)
end

function core.on_change(self)
if self.context and (not self.context:changed()) then
self.context = require("neocomplete.context").new(self.context)
if not self.context:changed() then
return
end
self:complete()
self:complete(1)
end

return core
6 changes: 5 additions & 1 deletion lua/neocomplete/lsp_source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ function lsp_source.new(client)
params.context = context.completion_context
---@type lsp.CompletionItem
local items
local is_incomplete
client.request(vim.lsp.protocol.Methods.textDocument_completion, params, function(err, result)
if err then
vim.print(err)
end
if result then
if result.isIncomplete then
is_incomplete = true
end
items = get_items(result)
end
callback(items)
callback(items, is_incomplete)
end)
end,
get_trigger_characters = function()
Expand Down
Loading

0 comments on commit 5cfed8b

Please sign in to comment.