Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra terminal cells styled after end of file #1176

Open
git-bruh opened this issue Apr 5, 2024 · 3 comments
Open

Extra terminal cells styled after end of file #1176

git-bruh opened this issue Apr 5, 2024 · 3 comments

Comments

@git-bruh
Copy link
Contributor

git-bruh commented Apr 5, 2024

Problem

For markdown files containing a hyperlink at the end (no newlines), the underlining style overflows over to the empty cells:

image

I've not investigated much but there might be an off by one error around this logic (as lua is one indexed):

local token_end = lex_start + (tokens[#tokens] or 1) - 1

Seems similar to #1147

Steps to reproduce

No response

vis version (vis -v)

vis 95bf9f5-dirty +curses +lua +lpeg

Terminal name/version

foot 1.16.2

$TERM environment variable

foot

@acidrums4
Copy link

Not sure if it's related, but if for example I set a different background color for a certain type of string in the theme file (say, lexers.STYLE_FUNCTION), the background color will overflow over the neighboring empty cells too.

@git-bruh
Copy link
Contributor Author

git-bruh commented Apr 6, 2024

diff --git a/view.c b/view.c
index df7906b..6a723a4 100644
--- a/view.c
+++ b/view.c
@@ -1432,10 +1432,10 @@ void view_style(View *view, enum UiStyle style, size_t start, size_t end) {
 		col++;
 
 	do {
-		while (pos <= end && col < width) {
+		while (pos < end && col < width) {
 			pos += line->cells[col].len;
 			view->ui->style_set(view->ui, &line->cells[col++], style);
 		}
 		col = 0;
-	} while (pos <= end && (line = line->next));
+	} while (pos < end && (line = line->next));
 }

This works, the end position passed from lua is equal to view->end, and we should only iterate till view->end - 1

Otherwise we reach view->end which has empty columns, and adding line->cells[col].len to pos keeps the loop running as length of an empty cell is 0

@acidrums4 could you see if this fixes your issue as well?

EDIT: nvm, this breaks regular highlighting at the last character as well, in case there is more text after it , eg [a](b) xyz, the ) isn't highlighted

@git-bruh
Copy link
Contributor Author

git-bruh commented Apr 6, 2024

This might be a more reasonable fix:

diff --git a/lua/vis-std.lua b/lua/vis-std.lua
index 8b5715b..1ccef0d 100644
--- a/lua/vis-std.lua
+++ b/lua/vis-std.lua
@@ -75,7 +75,7 @@ vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
 	local data = win.file:content(viewport)
 	local token_styles = lexer._TOKENSTYLES
 	local tokens = lexer:lex(data, 1)
-	local token_end = lex_start + (tokens[#tokens] or 1) - 1
+	local token_end = lex_start + ((tokens[#tokens] and tokens[#tokens] - 1) or 1) - 1
 
 	for i = #tokens - 1, 1, -2 do
 		local token_start = lex_start + (tokens[i-1] or 1) - 1

Not sure if I'm missing any edge case here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants