Skip to content

Commit 648947a

Browse files
committed
fix(formatter): get more correct markdown types
Instead of just using the filename extension, use the neovim filetype (with some specific overrides). Fall back to the filename extension. This means we render our markdown sections with `markdown` as the type instead of `md` and that enables rendering embedded codefences correctly. Hopefully the last fix on this :)
1 parent 27396b0 commit 648947a

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

lua/opencode/context.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ end
269269

270270
---@param selection OpencodeContextSelection
271271
local function format_selection_part(selection)
272-
local lang = selection.file and selection.file.extension or ''
272+
local lang = util.get_markdown_filetype(selection.file and selection.file.name or '') or ''
273273

274274
return {
275275
type = 'text',
276276
text = vim.json.encode({
277277
context_type = 'selection',
278278
file = selection.file,
279-
content = string.format('```%s\n%s\n```', lang, selection.content),
279+
content = string.format('`````%s\n%s\n`````', lang, selection.content),
280280
lines = selection.lines,
281281
}),
282282
synthetic = true,

lua/opencode/ui/formatter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ end
428428
---@param metadata FileToolMetadata Metadata for the tool use
429429
function M._format_file_tool(output, tool_type, input, metadata)
430430
local file_name = input and vim.fn.fnamemodify(input.filePath, ':t') or ''
431-
local file_type = input and vim.fn.fnamemodify(input.filePath, ':e') or ''
431+
local file_type = input and util.get_markdown_filetype(input.filePath) or ''
432432
local tool_action_icons = { read = icons.get('read'), edit = icons.get('edit'), write = icons.get('write') }
433433

434434
M._format_action(output, tool_action_icons[tool_type] .. ' ' .. tool_type, file_name)

lua/opencode/util.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,30 @@ function M.parse_dot_args(args_str)
361361
return result
362362
end
363363

364+
--- Get the markdown type to use based on the filename. First gets the neovim type
365+
--- for the file. Then apply any specific overrides. Falls back to using the file
366+
--- extension if nothing else matches
367+
--- @param filename string filename, possibly including path
368+
--- @return string markdown_filetype
369+
function M.get_markdown_filetype(filename)
370+
local file_type_overrides = {
371+
javascriptreact = 'jsx',
372+
typescriptreact = 'tsx',
373+
sh = 'bash',
374+
yaml = 'yml',
375+
}
376+
377+
local file_type = vim.filetype.match({ filename = filename }) or ''
378+
379+
if file_type_overrides[file_type] then
380+
return file_type_overrides[file_type]
381+
end
382+
383+
if file_type and file_type ~= '' then
384+
return file_type
385+
end
386+
387+
return vim.fn.fnamemodify(filename, ':e')
388+
end
389+
364390
return M

0 commit comments

Comments
 (0)