Skip to content

Commit

Permalink
feat(renderer): Added basic language names to code blocks
Browse files Browse the repository at this point in the history
Taken from Github syntax support page. A lot of them may be useless.

Ref: #72
  • Loading branch information
OXY2DEV committed Aug 3, 2024
1 parent 3b270c1 commit c9b4f77
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 6 deletions.
221 changes: 221 additions & 0 deletions lua/markview/languages.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
local languages = {};

languages.patterns = {
{ ".*%.feature", "Cucumber" },

{ ".*%.abap", "Abap" },

{ ".*%.ada", "Ada" },
{ "adb", "Ada" },
{ "ads$", "Ada" },

{ "htaccess", "Apacheconf" },
{ "apache.conf", "Apacheconf" },
{ "apache2.conf", "Apacheconf" },

{ ".*%.as", "As" },
{ ".%*%.asy", "Asy" },

{ "ahkl", "Ahk" },

{ "sh", "Bash" },
{ "ksh", "Bash" },
{ "ksh", "Bash" },
{ ".*%.ebuild", "Bash" },
{ ".*%.eclass", "Bash" },

{ ".*%.befunge", "Befunge" },
{ ".*%.bmx", "Blitzmax" },
{ ".*%.boo", "Boo" },

{ "bf", "Brainfuck" },
{ "b", "Brainfuck" },

{ "cmd", "Bat" },

{ ".*%.cfc", "Cfm" },
{ "cfml", "Cfm" },

{ "cl", "Cl" },
{ "lisp", "Cl" },
{ ".*%.el", "Cl" },

{ "clj", "Clojure" },
{ "cljs", "Clojure" },

{ "h", "C" },
{ "c", "C" },
{ ".*.cmake", "Cmake" },

{ "coffee", "Cofeescript" },
{ ".*%.sh%-session", "Console" },

{ "cpp", "C++" },
{ "hpp", "C++" },
{ "cc", "C++" },
{ "hh", "C++" },
{ "cxx", "C++" },
{ "hxx", "C++" },
{ ".*%.pde", "C++" },

{ "csharp", "C#" },
{ "cs", "C#" },
{ ".*.%cs", "C#" },

{ "css", "CSS" },

{ "pyx", "Cython" },
{ "pxd", "Cython" },
{ ".*%.pxi", "Cython" },

{ "d", "D" },
{ "di", "D" },

{ ".*%.pas", "Delphi" },

{ "patch", "Diff" },

{ "darcspatch", "Dpatch" },

{ "jbst", "Duel" },

{ "dyl", "Dylan" },

{ ".*%.erb", "Erb" },

{ ".*%.erl%-sh", "Erl" },

{ "erl", "Erlang" },
{ "hrl", "Erlang" },

{ "flx", "Felix" },
{ "flxh", "Felix" },

{ "f", "Fortran" },
{ "f90", "Fortran" },

{ "s", "Gas" },
{ "S", "Gas" },

{ ".*%.kid", "Genshi" },

{ "gitignore", "Gitignore" },

{ "vert", "GLSL" },
{ "frag", "GLSL" },
{ ".*%.geo", "GLSL" },

{ "plot", "Gnuplot" },
{ "plt", "Gnuplot" },

{ ".*%.go", "Go" },

{ "hs", "Haskell" },
{ ".*%.hs", "Haskell" },

{ "htm", "Html" },
{ "xhtml", "Html" },
{ "xslt", "Html" },

{ ".*%.hx", "Hx" },

{ "hy", "Hybris" },
{ "hyb", "Hybris" },

{ "ini", "INI" },
{ "cfg", "INI" },

{ ".*%.io", "Io" },
{ "ik", "Ioke" },

{ ".*%.jade", "Jade" },

{ ".*%.Java", "Java" },

{ "ll", "LLVM" },

{ "wlua", "Lua" },

{ "mak", "Make" },
{ "makefile", "Make" },
{ "Makefile", "Make" },
{ "GNUmakefile", "Make" },

{ ".*%.mao", "Make" },

{ "mhtml", "Mason" },
{ "mc", "Mason" },
{ ".*%.mi", "Mason" },
{ "autohandler", "Mason" },
{ "dhandler", "Mason" },

{ "md", "Markdown" },

{ "m", "Objective C" },
{ ".*%.m", "Objective C" },
{ ".*%.j", "Objective J" },

{ "ml", "Ocaml" },
{ "mli", "Ocaml" },
{ "mll", "Ocaml" },
{ "mly", "Ocaml" },

{ "pm", "Perl" },
{ "pl", "Perl" },

{ "php", "PHP" },

{ "ps", "Postscript" },
{ "eps", "Postscript" },

{ "pro", "Prolog" },
{ ".*%.pl", "Prolog" },

{ ".*%.properties", "Properties" },

{ ".*%.py3tb", "Py3tb" },
{ ".*%.pytb", "Pytb" },
{ "py", "Python" },
{ "pyw", "Python" },
{ "sc", "Python" },
{ "SConstruct", "Python" },
{ "SConscript", "Python" },
{ "tac", "Python" },

{ ".*%.R", "R" },

{ "rb", "Ruby" },
{ "rbw", "Ruby" },
{ "RakeFile", "Ruby" },
{ "rake", "Ruby" },
{ "gemspec", "Ruby" },
{ "rbx", "Ruby" },
{ "duby", "Ruby" },

{ "rs", "rust" },

{ "sql", "SQL" },
{ ".*%.sql", "SQL" },

{ "txt", "Text" },

{ "ts", "Typescript" },

{ "yml", "Yaml" },
};

languages.get_name = function (name)
if not name or name == "" then
return "Unknown";
end

for _, pattern in ipairs(languages.patterns) do
if name:match("^" .. pattern[1] .. "$") then
return pattern[2];
end
end

return string.gsub(name, "^%l", string.upper);
end

return languages;
17 changes: 11 additions & 6 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local renderer = {};
local devicons = require("nvim-web-devicons");

local entites = require("markview.entites");
local utils = require("markview.utils");
local languages = require("markview.languages");


_G.__markview_views = {};
Expand Down Expand Up @@ -972,27 +972,32 @@ renderer.render_code_blocks = function (buffer, content, config_table)
local icon, hl = devicons.get_icon(nil, language, { default = true });
local block_length = content.largest_line;

local languageName;

if config_table.language_names ~= nil then
for _, lang in ipairs(config_table.language_names) do
if language == lang[1] then
language = lang[2];
break;
languageName = lang[2];
goto nameFound;
end
end
end

languageName = languages.get_name(language)
::nameFound::

if type(config_table.min_width) == "number" and config_table.min_width > block_length then
block_length = config_table.min_width
end

local lang_width = vim.fn.strchars(" " .. icon .. " " .. (language == "" and "Unknown" or language) .. " ");
local lang_width = vim.fn.strchars(" " .. icon .. " " .. languageName .. " ");

if config_table.language_direction == nil or config_table.language_direction == "left" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start + 3 + vim.fn.strlen(content.language), {
virt_text_pos = config_table.position or "inline",
virt_text = {
{ " " .. icon .. " ", set_hl(hl) },
{ (language == "" and "Unknown" or language) .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
{ languageName .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
{ string.rep(config_table.pad_char or " ", block_length - lang_width + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
},

Expand All @@ -1007,7 +1012,7 @@ renderer.render_code_blocks = function (buffer, content, config_table)
virt_text = {
{ string.rep(config_table.pad_char or " ", block_length - lang_width + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
{ " " .. icon .. " ", set_hl(hl) },
{ (language == "" and "Unknown" or language) .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
{ languageName .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
},

sign_text = config_table.sign == true and icon or nil,
Expand Down

0 comments on commit c9b4f77

Please sign in to comment.