aboutsummaryrefslogtreecommitdiffstats
path: root/app/Comment.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-18 07:38:22 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-18 07:38:24 +0100
commitc8ab97e77c8ab56b9835d9f260dc222a10e9b3c6 (patch)
treeedeffd14127196b19e6205ba71b4abe3d0e00a09 /app/Comment.hs
parent4fe90ed3e41fb54a65e203719824d0fcf272909f (diff)
feat: add support for c, elm, nix, shell
Diffstat (limited to 'app/Comment.hs')
-rw-r--r--app/Comment.hs28
1 files changed, 18 insertions, 10 deletions
diff --git a/app/Comment.hs b/app/Comment.hs
index 2769c83..123acec 100644
--- a/app/Comment.hs
+++ b/app/Comment.hs
@@ -9,7 +9,7 @@ module Comment
)
where
-import Comment.Language
+import Comment.Language qualified as L
import Control.Applicative (liftA2)
import Control.Exception (catch)
import Data.Binary (Binary)
@@ -32,7 +32,7 @@ import TreeSitter qualified as S
data Comment = Comment
{ text :: T.Text,
- language :: Language,
+ language :: L.Language,
startByte :: Int,
endByte :: Int,
startPoint :: Point,
@@ -50,12 +50,20 @@ data Point = Point
getComments :: Git.CommitHash -> FilePath -> IO [Comment]
getComments commitHash filePath =
fmap mergeLineComments
- . (extractComments filePath language . LB.toStrict)
+ . ( extractComments
+ filePath
+ ( -- TODO Support amiguous file languages
+ --
+ -- @backlog
+ N.head language
+ )
+ . LB.toStrict
+ )
=<< catch
(Git.readTextFileOfBS commitHash filePath)
(\(_ :: E.CannotReadFile) -> pure "")
where
- language = fromExtension (takeExtension filePath)
+ language = L.fromPath (takeExtension filePath)
mergeLineComments :: [Comment] -> [Comment]
mergeLineComments =
@@ -86,13 +94,13 @@ getComments commitHash filePath =
| p a x = go ((x N.:| a : as) : rs) xs
| otherwise = go (N.singleton x : ass) xs
-extractComments :: FilePath -> Language -> B.ByteString -> IO [Comment]
+extractComments :: FilePath -> L.Language -> B.ByteString -> IO [Comment]
extractComments filePath language str' =
alloca $ \nodesPtrPtr -> do
alloca $ \numNodesPtr -> do
B.useAsCString str' $ \str ->
S.extract_comments
- (parser language)
+ (L.parser language)
str
nodesPtrPtr
numNodesPtr
@@ -135,11 +143,11 @@ comment :: CommentStyle -> T.Text -> T.Text
comment (LineStyle linePrefix) = T.unlines . map ((linePrefix <> " ") <>) . T.lines
comment (BlockStyle blockStart blockEnd) = (blockStart <>) . (<> blockEnd)
-uncomment :: Language -> T.Text -> (CommentStyle, T.Text)
+uncomment :: L.Language -> T.Text -> (CommentStyle, T.Text)
uncomment language rawText =
maybe
- ( ( LineStyle (lineStart language),
- stripLineComments (lineStart language) text
+ ( ( LineStyle (L.lineStart language),
+ stripLineComments (L.lineStart language) text
)
)
( \(blockStart, blockEnd) ->
@@ -148,7 +156,7 @@ uncomment language rawText =
)
)
$ do
- (blockStarts, blockEnd) <- block language
+ (blockStarts, blockEnd) <- L.block language
(,blockEnd) <$> find (`T.isPrefixOf` text) blockStarts
where
text = stripLines rawText