Skip to content

Commit 7795912

Browse files
cameronrsudo-tee
authored andcommitted
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 3119cae commit 7795912

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
@@ -389,4 +389,30 @@ function M.check_prompt_allowed(guard_callback, mentioned_files)
389389
return result, nil
390390
end
391391

392+
--- Get the markdown type to use based on the filename. First gets the neovim type
393+
--- for the file. Then apply any specific overrides. Falls back to using the file
394+
--- extension if nothing else matches
395+
--- @param filename string filename, possibly including path
396+
--- @return string markdown_filetype
397+
function M.get_markdown_filetype(filename)
398+
local file_type_overrides = {
399+
javascriptreact = 'jsx',
400+
typescriptreact = 'tsx',
401+
sh = 'bash',
402+
yaml = 'yml',
403+
}
404+
405+
local file_type = vim.filetype.match({ filename = filename }) or ''
406+
407+
if file_type_overrides[file_type] then
408+
return file_type_overrides[file_type]
409+
end
410+
411+
if file_type and file_type ~= '' then
412+
return file_type
413+
end
414+
415+
return vim.fn.fnamemodify(filename, ':e')
416+
end
417+
392418
return M

0 commit comments

Comments
 (0)