Skip to content

Commit b2d857c

Browse files
chore(refactor): add view module
## Details Moves list of Range associated with a buffer and its windows into a new `View` module. Stores the various overlap, contain, and each methods within `View`. Store an instance of `View` in the top level `Context`. Store `Context`, `Conceal`, & `View` in new request directory, meant to encapsulate the state of a render cycle. Add string representations for `Range` & `View`.
1 parent 57c7f33 commit b2d857c

File tree

17 files changed

+199
-141
lines changed

17 files changed

+199
-141
lines changed

lua/render-markdown/core/ui.lua

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local Buffer = require('render-markdown.lib.buffer')
22
local Compat = require('render-markdown.lib.compat')
3-
local Context = require('render-markdown.lib.context')
3+
local Context = require('render-markdown.request.context')
44
local Env = require('render-markdown.lib.env')
55
local Extmark = require('render-markdown.lib.extmark')
66
local Iter = require('render-markdown.lib.iter')
@@ -108,19 +108,15 @@ function M.run_update(buf, win, change)
108108
local initial = buffer:initial()
109109
if initial or parse then
110110
M.clear_buffer(buf, buffer)
111-
buffer:set_marks(M.parse_buffer({
112-
buf = buf,
113-
win = win,
114-
config = config,
115-
mode = mode,
116-
}))
111+
local extmarks = M.parse_buffer(buf, win, config, mode)
112+
buffer:set_marks(extmarks)
113+
if initial then
114+
Compat.fix_lsp_window(buf, win, extmarks)
115+
M.config.on.initial({ buf = buf, win = win })
116+
end
117117
end
118118
local range = config:hidden(mode, row)
119119
local extmarks = buffer:get_marks()
120-
if initial then
121-
Compat.fix_lsp_window(buf, win, extmarks)
122-
M.config.on.initial({ buf = buf, win = win })
123-
end
124120
for _, extmark in ipairs(extmarks) do
125121
if extmark:get().conceal and extmark:overlaps(range) then
126122
extmark:hide(M.ns, buf)
@@ -154,19 +150,21 @@ function M.clear_buffer(buf, buffer)
154150
end
155151

156152
---@private
157-
---@param props render.md.context.Props
153+
---@param buf integer
154+
---@param win integer
155+
---@param config render.md.main.Config
156+
---@param mode string
158157
---@return render.md.Extmark[]
159-
function M.parse_buffer(props)
160-
local buf = props.buf
158+
function M.parse_buffer(buf, win, config, mode)
161159
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)
162160
if not has_parser or not parser then
163161
log.buf('error', 'fail', buf, 'no treesitter parser found')
164162
return {}
165163
end
166164
-- reset buffer context
167-
local context = Context.reset(props)
165+
local context = Context.start(buf, win, config, mode)
168166
-- make sure injections are processed
169-
context:parse(parser)
167+
context.view:parse(parser)
170168
-- parse markdown after other nodes to get accurate state
171169
local marks = {} ---@type render.md.Mark[]
172170
local markdown = {} ---@type render.md.handler.Context[]
@@ -189,13 +187,13 @@ end
189187
---Run user & builtin handlers when available. User handler is always executed,
190188
---builtin handler is skipped if user handler does not specify extends.
191189
---@private
192-
---@param context render.md.Context
190+
---@param context render.md.request.Context
193191
---@param ctx render.md.handler.Context
194192
---@param language string
195193
---@return render.md.Mark[]
196194
function M.parse_tree(context, ctx, language)
197195
log.buf('debug', 'language', ctx.buf, language)
198-
if not context:overlaps(ctx.root) then
196+
if not context.view:overlaps(ctx.root) then
199197
return {}
200198
end
201199

lua/render-markdown/handler/html.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
local Context = require('render-markdown.lib.context')
1+
local Context = require('render-markdown.request.context')
22
local Marks = require('render-markdown.lib.marks')
33
local ts = require('render-markdown.core.ts')
44

55
---@class render.md.handler.buf.Html
66
---@field private query vim.treesitter.Query
77
---@field private renderers table<string, render.md.Render>
8-
---@field private context render.md.Context
8+
---@field private context render.md.request.Context
99
local Handler = {}
1010
Handler.__index = Handler
1111

@@ -35,7 +35,7 @@ function Handler:parse(root)
3535
return {}
3636
end
3737
local marks = Marks.new(self.context, true)
38-
self.context:query(root, self.query, function(capture, node)
38+
self.context.view:query(root, self.query, function(capture, node)
3939
local renderer = self.renderers[capture]
4040
assert(renderer ~= nil, 'unhandled html capture: ' .. capture)
4141
local render = renderer:new(self.context, marks, node)

lua/render-markdown/handler/latex.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local Context = require('render-markdown.lib.context')
1+
local Context = require('render-markdown.request.context')
22
local Iter = require('render-markdown.lib.iter')
33
local Marks = require('render-markdown.lib.marks')
44
local Node = require('render-markdown.lib.node')

lua/render-markdown/handler/markdown.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
local Context = require('render-markdown.lib.context')
1+
local Context = require('render-markdown.request.context')
22
local Marks = require('render-markdown.lib.marks')
33
local ts = require('render-markdown.core.ts')
44

55
---@class render.md.handler.buf.Markdown
66
---@field private query vim.treesitter.Query
77
---@field private renderers table<string, render.md.Render>
8-
---@field private context render.md.Context
8+
---@field private context render.md.request.Context
99
local Handler = {}
1010
Handler.__index = Handler
1111

@@ -67,7 +67,7 @@ end
6767
---@return render.md.Mark[]
6868
function Handler:parse(root)
6969
local marks = Marks.new(self.context, false)
70-
self.context:query(root, self.query, function(capture, node)
70+
self.context.view:query(root, self.query, function(capture, node)
7171
local renderer = self.renderers[capture]
7272
assert(renderer ~= nil, 'unhandled markdown capture: ' .. capture)
7373
local render = renderer:new(self.context, marks, node)

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
local Context = require('render-markdown.lib.context')
1+
local Context = require('render-markdown.request.context')
22
local Marks = require('render-markdown.lib.marks')
33
local ts = require('render-markdown.core.ts')
44

55
---@class render.md.handler.buf.MarkdownInline
66
---@field private query vim.treesitter.Query
77
---@field private renderers table<string, render.md.Render>
8-
---@field private context render.md.Context
8+
---@field private context render.md.request.Context
99
local Handler = {}
1010
Handler.__index = Handler
1111

@@ -46,7 +46,7 @@ end
4646
---@return render.md.Mark[]
4747
function Handler:parse(root)
4848
local marks = Marks.new(self.context, true)
49-
self.context:query(root, self.query, function(capture, node)
49+
self.context.view:query(root, self.query, function(capture, node)
5050
local renderer = self.renderers[capture]
5151
assert(renderer ~= nil, 'unhandled inline capture: ' .. capture)
5252
local render = renderer:new(self.context, marks, node)

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.3.23'
8+
M.version = '8.3.24'
99

1010
function M.check()
1111
M.start('version')

lua/render-markdown/lib/indent.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
local Str = require('render-markdown.lib.str')
22

33
---@class render.md.Indent
4-
---@field private context render.md.Context
4+
---@field private context render.md.request.Context
55
---@field private config render.md.indent.Config
66
---@field private node render.md.Node
77
local Indent = {}
88
Indent.__index = Indent
99

10-
---@param context render.md.Context
10+
---@param context render.md.request.Context
1111
---@param node render.md.Node
1212
---@return render.md.Indent
1313
function Indent.new(context, node)

lua/render-markdown/lib/marks.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ local log = require('render-markdown.core.log')
2626
---@alias render.md.mark.Element boolean|render.md.Element
2727

2828
---@class render.md.Marks
29-
---@field private context render.md.Context
29+
---@field private context render.md.request.Context
3030
---@field private ignore render.md.conceal.Ignore
3131
---@field private update boolean
3232
---@field private marks render.md.Mark[]
3333
local Marks = {}
3434
Marks.__index = Marks
3535

36-
---@param context render.md.Context
36+
---@param context render.md.request.Context
3737
---@param update boolean
3838
---@return render.md.Marks
3939
function Marks.new(context, update)

lua/render-markdown/lib/range.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ function Range.__lt(a, b)
2525
end
2626
end
2727

28+
---@return string
29+
function Range:__tostring()
30+
return ('%d->%d'):format(self.top, self.bottom)
31+
end
32+
2833
---@param top integer
2934
---@param bottom integer
3035
---@return boolean

lua/render-markdown/render/base.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local Str = require('render-markdown.lib.str')
33
local colors = require('render-markdown.core.colors')
44

55
---@class render.md.Render
6-
---@field protected context render.md.Context
6+
---@field protected context render.md.request.Context
77
---@field protected config render.md.main.Config
88
---@field protected marks render.md.Marks
99
---@field protected node render.md.Node
@@ -12,7 +12,7 @@ local colors = require('render-markdown.core.colors')
1212
local Base = {}
1313
Base.__index = Base
1414

15-
---@param context render.md.Context
15+
---@param context render.md.request.Context
1616
---@param marks render.md.Marks
1717
---@param node render.md.Node
1818
---@return render.md.Render

0 commit comments

Comments
 (0)