Skip to content

Commit

Permalink
Extract code languages into separate file (#1599)
Browse files Browse the repository at this point in the history
Fixes #1585

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AndreasArvidsson and pre-commit-ci[bot] authored Nov 16, 2024
1 parent 760ef80 commit cb54f25
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 91 deletions.
77 changes: 77 additions & 0 deletions core/modes/code_languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Language:
id: str
spoken_forms: list[str]
extensions: list[str]

def __init__(self, id: str, spoken_form: str | list[str], extensions: list[str]):
self.id = id
self.spoken_forms = (
[spoken_form] if isinstance(spoken_form, str) else spoken_form
)
self.extensions = extensions


# Maps code language identifiers, names and file extensions. Only put languages
# here which have a supported language mode; that's why there are so many
# commented out entries.
code_languages = [
# Language("assembly", "assembly", ["asm", "s"]),
# Language("bash", "bash", ["sh", "bashbook"]),
Language("batch", "batch", ["bat"]),
Language("c", "see", ["c", "h"]),
# Language("cmake", "see make", ["cmake"]),
# Language("cplusplus", "see plus plus", ["cpp", "hpp"]),
Language("csharp", "see sharp", ["cs"]),
Language("css", "c s s", ["css"]),
# Language("elisp", "elisp", ["el"]),
Language("elixir", "elixir", ["ex"]),
# Language("elm", "elm", ["elm"]),
Language("gdb", "g d b", ["gdb"]),
Language("go", ["go lang", "go language"], ["go"]),
# html doesn't actually have a language mode, but we do have snippets.
Language("html", "html", ["html"]),
Language("java", "java", ["java"]),
Language("javascript", "java script", ["js"]),
Language("javascriptreact", "java script react", ["jsx"]),
# Language("json", "json", ["json"]),
# Language("jsonl", "json lines", ["jsonl"]),
Language("kotlin", "kotlin", ["kt"]),
Language("lua", "lua", ["lua"]),
Language("markdown", "mark down", ["md"]),
# Language("perl", "perl", ["pl"]),
Language("php", "p h p", ["php"]),
# Language("powershell", "power shell", ["ps1"]),
Language("protobuf", "proto buf", ["proto"]),
Language("python", "python", ["py"]),
Language("r", "are language", ["r"]),
# Language("racket", "racket", ["rkt"]),
Language("ruby", "ruby", ["rb"]),
Language("rust", "rust", ["rs"]),
Language("scala", "scala", ["scala"]),
Language("scss", "scss", ["scss"]),
# Language("snippets", "snippets", ["snippets"]),
Language("sql", "sql", ["sql"]),
Language("stata", "stata", ["do", "ado"]),
Language("talon", "talon", ["talon"]),
Language("talonlist", "talon list", ["talon-list"]),
Language("terraform", "terraform", ["tf"]),
Language("tex", ["tech", "lay tech", "latex"], ["tex"]),
Language("typescript", "type script", ["ts"]),
Language("typescriptreact", "type script react", ["tsx"]),
# Language("vba", "vba", ["vba"]),
Language("vimscript", "vim script", ["vim", "vimrc"]),
]

# Files without specific extensions but are associated with languages
# Maps full filename to language identifiers
code_special_file_map = {
"CMakeLists.txt": "cmake",
"Makefile": "make",
"Dockerfile": "docker",
"meson.build": "meson",
".bashrc": "bash",
".zshrc": "zsh",
"PKGBUILD": "pkgbuild",
".vimrc": "vimscript",
"vimrc": "vimscript",
}
99 changes: 12 additions & 87 deletions core/modes/language_modes.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,6 @@
from talon import Context, Module, actions, app

# Maps language mode names to the extensions that activate them. Only put things
# here which have a supported language mode; that's why there are so many
# commented out entries. TODO: make this a csv file?
language_extensions = {
# 'assembly': 'asm s',
# 'bash': 'bashbook sh',
"batch": "bat",
"c": "c h",
# 'cmake': 'cmake',
# "cplusplus": "cpp hpp",
"csharp": "cs",
"css": "css",
# 'elisp': 'el',
# 'elm': 'elm',
"gdb": "gdb",
"go": "go",
"java": "java",
"javascript": "js",
"javascriptreact": "jsx",
# "json": "json",
"elixir": "ex",
"kotlin": "kt",
"lua": "lua",
"markdown": "md",
# 'perl': 'pl',
"php": "php",
# 'powershell': 'ps1',
"python": "py",
"protobuf": "proto",
"r": "r",
# 'racket': 'rkt',
"ruby": "rb",
"rust": "rs",
"scala": "scala",
"scss": "scss",
# 'snippets': 'snippets',
"sql": "sql",
"stata": "do ado",
"talon": "talon",
"talonlist": "talon-list",
"terraform": "tf",
"tex": "tex",
"typescript": "ts",
"typescriptreact": "tsx",
# 'vba': 'vba',
"vimscript": "vim vimrc",
# html doesn't actually have a language mode, but we do have snippets.
"html": "html",
}

# Files without specific extensions but are associated with languages
special_file_map = {
"CMakeLists.txt": "cmake",
"Makefile": "make",
"Dockerfile": "docker",
"meson.build": "meson",
".bashrc": "bash",
".zshrc": "zsh",
"PKGBUILD": "pkgbuild",
".vimrc": "vimscript",
"vimrc": "vimscript",
}

# Override speakable forms for language modes. If not present, a language mode's
# name is used directly.
language_name_overrides = {
"cplusplus": ["see plus plus"],
"csharp": ["see sharp"],
"css": ["c s s"],
"gdb": ["g d b"],
"go": ["go", "go lang", "go language"],
"r": ["are language"],
"tex": ["tech", "lay tech", "latex"],
}
from .code_languages import code_languages, code_special_file_map

mod = Module()
ctx = Context()
Expand All @@ -87,30 +14,28 @@
mod.tag("code_language_forced", "This tag is active when a language mode is forced")
mod.list("language_mode", desc="Name of a programming language mode.")

ctx.lists["self.language_mode"] = {
name: language
for language in language_extensions
for name in language_name_overrides.get(language, [language])
# Maps spoken forms to language ids
ctx.lists["user.language_mode"] = {
spoken_form: language.id
for language in code_languages
for spoken_form in language.spoken_forms
}

# Maps extension to languages.
# Maps extension to language ids
extension_lang_map = {
"." + ext: language
for language, extensions in language_extensions.items()
for ext in extensions.split()
f".{ext}": lang.id for lang in code_languages for ext in lang.extensions
}

language_ids = set(language_extensions.keys())

language_ids = {lang.id for lang in code_languages}
forced_language = ""


@ctx.action_class("code")
class CodeActions:
def language():
file_name = actions.win.filename()
if file_name in special_file_map:
return special_file_map[file_name]
if file_name in code_special_file_map:
return code_special_file_map[file_name]

file_extension = actions.win.file_ext()
return extension_lang_map.get(file_extension, "")
Expand All @@ -127,7 +52,7 @@ class Actions:
def code_set_language_mode(language: str):
"""Sets the active language mode, and disables extension matching"""
global forced_language
assert language in language_extensions
assert language in language_ids
forced_language = language
# Update tags to force a context refresh. Otherwise `code.language` will not update.
# Necessary to first set an empty list otherwise you can't move from one forced language to another.
Expand Down
8 changes: 4 additions & 4 deletions core/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from talon import Context, Module, actions, app, fs, settings

from ..modes.language_modes import language_ids
from ..modes.code_languages import code_languages
from .snippet_types import InsertionSnippet, Snippet, WrapperSnippet
from .snippets_parser import create_snippets_from_file

Expand All @@ -30,10 +30,10 @@
snippets_map = {}

# Create a context for each defined language
for lang in language_ids:
for lang in code_languages:
ctx = Context()
ctx.matches = f"code.language: {lang}"
context_map[lang] = ctx
ctx.matches = f"code.language: {lang.id}"
context_map[lang.id] = ctx


def get_setting_dir():
Expand Down

0 comments on commit cb54f25

Please sign in to comment.