From c8ab97e77c8ab56b9835d9f260dc222a10e9b3c6 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Mon, 18 Dec 2023 07:38:22 +0100 Subject: feat: add support for c, elm, nix, shell --- app/Comment/Language.hs | 51 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'app/Comment/Language.hs') diff --git a/app/Comment/Language.hs b/app/Comment/Language.hs index 925cf56..7a9963f 100644 --- a/app/Comment/Language.hs +++ b/app/Comment/Language.hs @@ -1,6 +1,8 @@ +{-# LANGUAGE DerivingStrategies #-} + module Comment.Language ( Language (..), - fromExtension, + fromPath, parser, lineStart, block, @@ -10,31 +12,54 @@ where import Control.Exception (throw) import Data.Binary (Binary) +import Data.Languages qualified as L +import Data.List.NonEmpty qualified as N +import Data.Maybe (fromMaybe) import Data.Text qualified as T import Exception qualified as E import Foreign.Ptr (Ptr) import GHC.Generics (Generic) import TreeSitter qualified as S -data Language - = Haskell - deriving (Eq, Show, Generic, Binary) +newtype Language = Language {languageKey :: L.LanguageKey} + deriving (Eq, Show, Generic) + deriving newtype (Binary) -fromExtension :: String -> Language -fromExtension ".hs" = Haskell -fromExtension ext = throw $ E.UnknownFileExtension ext +fromPath :: FilePath -> N.NonEmpty Language +fromPath fp = + fromMaybe (throw $ E.UnknownFile fp) + . N.nonEmpty + . map (Language . L.languageName) + $ L.languagesForPath fp --- TODO add languages elm, shell, nix +-- TODO Add support for all tree-sitter supported languages -- --- @supersedes add-support-for-all-tree-grepper-supported-files +-- @backlog parser :: Language -> Ptr S.Language -parser Haskell = S.tree_sitter_haskell +parser (Language "C") = S.tree_sitter_c +parser (Language "Elm") = S.tree_sitter_elm +parser (Language "Haskell") = S.tree_sitter_haskell +parser (Language "Nix") = S.tree_sitter_nix +parser (Language "Shell") = S.tree_sitter_bash +parser (Language {languageKey}) = throw (E.UnsupportedLanguage languageKey) lineStart :: Language -> T.Text -lineStart Haskell = "--" +lineStart (Language "C") = "//" +lineStart (Language "Elm") = "--" +lineStart (Language "Haskell") = "--" +lineStart (Language "Nix") = "#" +lineStart (Language "Shell") = "#" +lineStart (Language {languageKey}) = throw (E.UnsupportedLanguage languageKey) block :: Language -> Maybe ([T.Text], T.Text) -block Haskell = Just (["{-"], "-}") +block (Language "C") = Just (["/*"], "*/") +block (Language "Elm") = Just (["{-|", "{-"], "-}") +block (Language "Haskell") = Just (["{-"], "-}") +block (Language "Nix") = Just (["/*"], "*/") +block (Language "Shell") = Nothing +block (Language {languageKey}) = throw (E.UnsupportedLanguage languageKey) nodeTypes :: Language -> [String] -nodeTypes Haskell = ["comment"] +nodeTypes (Language "Haskell") = ["comment"] +nodeTypes (Language "Shell") = ["comment"] +nodeTypes (Language {languageKey}) = throw (E.UnsupportedLanguage languageKey) -- cgit v1.2.3