Skip to content

Commit

Permalink
Merge branch 'main' into inlay_hints
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed Jun 3, 2024
2 parents 96c658a + d3f33a9 commit c6cb9d6
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 95 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: lua_ls-typecheck

on:
push:
branches:
- "main"
pull_request:
branches:
- "main"

jobs:
build:
name: Type Check Code Base
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: stevearc/nvim-typecheck-action@v2
with:
level: Warning
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,32 @@ return {
-- E = { text = "E", prio = 5 },
-- },
-- gutterHints = {
-- -- prio is not currently used for gutter hints
-- G = { text = "G", prio = 1 },
-- gg = { text = "gg", prio = 1 },
-- PrevParagraph = { text = "{", prio = 1 },
-- NextParagraph = { text = "}", prio = 1 },
-- G = { text = "G", prio = 10 },
-- gg = { text = "gg", prio = 9 },
-- PrevParagraph = { text = "{", prio = 8 },
-- NextParagraph = { text = "}", prio = 8 },
-- },
},
}
```

## ⚙️ Config

- Items can be hidden by settings their priority to 0, if you want to hide the
entire virtual line. Set all elements to `prio = 0` in combination with the
- `hints` can be hidden by setting their priority to 0. If you want to hide the
entire virtual line, set all elements to `prio = 0` in combination with the
below.
- `showBlankVirtLine = false`
Setting this option will mean that if a Virtual Line would be blank it wont be
Setting this option will mean that if a Virtual Line would be blank it won't be
rendered
- highlightColor can be set in two ways:
- `gutterHints` can be hidden by setting their priority to 0.
- `highlightColor` can be set in two ways:

1. As a table containing a link property pointing to an existing highlight group (see `:highlight` for valid options).
2. As a table specifying custom highlight values, such as foreground and background colors. ([more info](<https://neovim.io/doc/user/api.html#nvim_set_hl()>))
1. As a table containing a link property pointing to an existing highlight group (see `:highlight` for valid options).
2. As a table specifying custom highlight values, such as foreground and background colors. ([more info](<https://neovim.io/doc/user/api.html#nvim_set_hl()>))

### Hint priorities

Any hints that could appear in the same place as others should have unique priorities to avoid conflicts.

## ❔Usage

Expand Down
29 changes: 0 additions & 29 deletions lua/precognition/ffi.lua

This file was deleted.

17 changes: 12 additions & 5 deletions lua/precognition/horizontal_motions.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
local utils = require("precognition.utils")
local cc = utils.char_classes

local M = {}

local supportedBrackets = {
Expand Down Expand Up @@ -31,6 +28,9 @@ end
---@param big_word boolean
---@return Precognition.PlaceLoc
function M.next_word_boundary(str, cursorcol, linelen, big_word)
local utils = require("precognition.utils")
local cc = utils.char_classes

local offset = cursorcol
local char = vim.fn.strcharpart(str, offset - 1, 1)
local c_class = utils.char_class(char, big_word)
Expand Down Expand Up @@ -62,14 +62,18 @@ function M.end_of_word(str, cursorcol, linelen, big_word)
if cursorcol >= linelen then
return 0
end
local utils = require("precognition.utils")
local cc = utils.char_classes

local offset = cursorcol
local char = vim.fn.strcharpart(str, offset - 1, 1)
local c_class = utils.char_class(char, big_word)
local next_char_class = utils.char_class(vim.fn.strcharpart(str, (offset - 1) + 1, 1), big_word)
local rev_offset

if
(c_class == cc.other and next_char_class ~= cc.other) or (next_char_class == cc.other and c_class ~= cc.other)
(c_class == cc.punctuation and next_char_class ~= cc.punctuation)
or (next_char_class == cc.punctuation and c_class ~= cc.punctuation)
then
offset = offset + 1
char = vim.fn.strcharpart(str, offset - 1, 1)
Expand Down Expand Up @@ -117,6 +121,9 @@ end
---@param big_word boolean
---@return Precognition.PlaceLoc
function M.prev_word_boundary(str, cursorcol, linelen, big_word)
local utils = require("precognition.utils")
local cc = utils.char_classes

local offset = cursorcol - 1
local char = vim.fn.strcharpart(str, offset - 1, 1)
local c_class = utils.char_class(char, big_word)
Expand All @@ -132,7 +139,7 @@ function M.prev_word_boundary(str, cursorcol, linelen, big_word)
while utils.char_class(char, big_word) == c_class and offset >= 0 do
offset = offset - 1
char = vim.fn.strcharpart(str, offset - 1, 1)
--if remaining string is whitespace, return nil_wrap
--if remaining string is whitespace, return 0
local remaining = string.sub(str, offset)
if remaining:match("^%s*$") and #remaining > 0 then
return 0
Expand Down
78 changes: 48 additions & 30 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
local hm = require("precognition.horizontal_motions")
local vm = require("precognition.vertical_motions")
local utils = require("precognition.utils")
local compat = require("precognition.compat")

local M = {}
Expand Down Expand Up @@ -80,11 +77,10 @@ local default = {
highlightColor = { link = "Comment" },
hints = defaultHintConfig,
gutterHints = {
--prio is not currentlt used for gutter hints
G = { text = "G", prio = 1 },
gg = { text = "gg", prio = 1 },
PrevParagraph = { text = "{", prio = 1 },
NextParagraph = { text = "}", prio = 1 },
G = { text = "G", prio = 10 },
gg = { text = "gg", prio = 9 },
PrevParagraph = { text = "{", prio = 8 },
NextParagraph = { text = "}", prio = 8 },
},
}

Expand Down Expand Up @@ -114,6 +110,7 @@ local gutter_group = "precognition_gutter"
---@param extra_padding Precognition.ExtraPadding[]
---@return table
local function build_virt_line(marks, line_len, extra_padding)
local utils = require("precognition.utils")
if not marks then
return {}
end
Expand Down Expand Up @@ -163,6 +160,7 @@ end

---@return Precognition.GutterHints
local function build_gutter_hints()
local vm = require("precognition.vertical_motions")
---@type Precognition.GutterHints
local gutter_hints = {
G = vm.file_end(),
Expand All @@ -178,37 +176,54 @@ end
---@return nil
local function apply_gutter_hints(gutter_hints, bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if utils.is_blacklisted_buffer(bufnr) then
if require("precognition.utils").is_blacklisted_buffer(bufnr) then
return
end

local gutter_table = {}
for hint, loc in pairs(gutter_hints) do
if config.gutterHints[hint] and loc ~= 0 and loc ~= nil then
if gutter_signs_cache[hint] then
vim.fn.sign_unplace(gutter_group, { id = gutter_signs_cache[hint].id })
gutter_signs_cache[hint] = nil
end
vim.fn.sign_define(gutter_name_prefix .. hint, {
text = config.gutterHints[hint].text,
texthl = "PrecognitionHighlight",
})
local ok, res = pcall(vim.fn.sign_place, 0, gutter_group, gutter_name_prefix .. hint, bufnr, {
lnum = loc,
priority = 100,
})
if ok then
gutter_signs_cache[hint] = { line = loc, id = res }
end
if not ok and loc ~= 0 then
vim.notify_once(
"Failed to place sign: " .. hint .. " at line " .. loc .. vim.inspect(res),
vim.log.levels.WARN
)
if gutter_signs_cache[hint] then
vim.fn.sign_unplace(gutter_group, { id = gutter_signs_cache[hint].id })
gutter_signs_cache[hint] = nil
end

local prio = config.gutterHints[hint].prio

-- Build table of valid and priorised gutter hints.
if loc ~= 0 and loc ~= nil and prio > 0 then
local existing = gutter_table[loc]
if not existing or existing.prio < prio then
gutter_table[loc] = { hint = hint, prio = prio }
end
end
end

-- Only render valid and prioritised gutter hints.
for loc, data in pairs(gutter_table) do
local hint = data.hint
local sign_name = gutter_name_prefix .. hint
vim.fn.sign_define(sign_name, {
text = config.gutterHints[hint].text,
texthl = "PrecognitionHighlight",
})
local ok, res = pcall(vim.fn.sign_place, 0, gutter_group, sign_name, bufnr, {
lnum = loc,
priority = 100,
})
if ok then
gutter_signs_cache[hint] = { line = loc, id = res }
end
if not ok and loc ~= 0 then
vim.notify_once(
"Failed to place sign: " .. hint .. " at line " .. loc .. vim.inspect(res),
vim.log.levels.WARN
)
end
end
end

local function display_marks()
local utils = require("precognition.utils")
local bufnr = vim.api.nvim_get_current_buf()
if utils.is_blacklisted_buffer(bufnr) then
return
Expand All @@ -229,6 +244,8 @@ local function display_marks()
-- local before_cursor_rev = string.reverse(before_cursor)
-- local under_cursor = vim.fn.strcharpart(cur_line, cursorcol - 1, 1)

local hm = require("precognition.horizontal_motions")

-- FIXME: Lua patterns don't play nice with utf-8, we need a better way to
-- get char offsets for more complex motions.
--
Expand Down Expand Up @@ -261,6 +278,7 @@ local function display_marks()
end
end
--multicharacter padding

utils.add_multibyte_padding(cur_line, extra_padding, line_len)

local virt_line = build_virt_line(virtual_line_marks, line_len, extra_padding)
Expand Down
20 changes: 12 additions & 8 deletions lua/precognition/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ local M = {}
---@enum cc
M.char_classes = {
whitespace = 0,
other = 1,
punctuation = 1,
word = 2,
emoji = 3,
other = "other",
UNKNOWN = -1,
}

---@param char string
---@param big_word boolean
---@return integer
---@return cc
function M.char_class(char, big_word)
assert(type(big_word) == "boolean", "big_word must be a boolean")
local cc = M.char_classes
local byte = string.byte(char)
if byte == nil then
return cc.other

if char == "" then
return cc.UNKNOWN
end
if char == " " or char == "\t" or char == "\0" then

if char == "\0" then
return cc.whitespace
end

local c_class = require("precognition.ffi").utf_class(byte)
local c_class = vim.fn.charclass(char)

if big_word and c_class ~= 0 then
return cc.other
return cc.punctuation
end

return c_class
Expand Down
27 changes: 15 additions & 12 deletions tests/precognition/char_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ local eq = assert.are.same
describe("static classes", function()
it("are set correctly", function()
eq(cc.whitespace, 0)
eq(cc.other, 1)
eq(cc.punctuation, 1)
eq(cc.word, 2)
eq(cc.emoji, 3)
eq(cc.other, "other")
eq(cc.UNKNOWN, -1)
end)
end)

Expand All @@ -30,6 +33,17 @@ describe("char classing", function()
eq(utils.char_class("@", false), 1)
eq(utils.char_class(".", false), 1)
end)

it("can class emoji characters", function()
eq(utils.char_class("🐱", false), 3)
eq(utils.char_class("😸", false), 3)
eq(utils.char_class("💩", false), 3)
end)

it("can class nerdfont characters", function()
eq(utils.char_class("", false), 2)
eq(utils.char_class("", false), 2)
end)
end)

describe("big_word classing", function()
Expand All @@ -51,17 +65,6 @@ describe("big_word classing", function()
eq(utils.char_class("@", true), 1)
eq(utils.char_class(".", true), 1)
end)

it("can class emoji characters", function()
eq(utils.char_class("🐱", false), 2)
eq(utils.char_class("😸", false), 2)
eq(utils.char_class("💩", false), 2)
end)

it("can class nerdfont characters", function()
eq(utils.char_class("", false), 2)
eq(utils.char_class("", false), 2)
end)
end)

describe("pad arrays", function()
Expand Down
Loading

0 comments on commit c6cb9d6

Please sign in to comment.