aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-08-19 14:24:53 +1000
committerYuchen Pei <hi@ypei.me>2022-08-19 14:24:53 +1000
commitfaa57eb57831a07a17a553a3ca230f55dd5260b7 (patch)
tree470dbc8fb9a094808dbe07db871adca75b31e915
parent915e96424778fe7d178e0ef1d7289a0a5cee2b4b (diff)
Adding tokenized lines to apitokenized-lines
-rw-r--r--src/HaskellCodeExplorer/Types.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/HaskellCodeExplorer/Types.hs b/src/HaskellCodeExplorer/Types.hs
index aac890b..8ed2eb1 100644
--- a/src/HaskellCodeExplorer/Types.hs
+++ b/src/HaskellCodeExplorer/Types.hs
@@ -627,11 +627,55 @@ instance A.ToJSON ModuleInfo where
[ ("id" , A.toJSON id)
, ("name" , A.toJSON name)
, ("sourceCodeHtml", A.toJSON . renderHtml $ html)
+ , ("tokenizedLines", A.toJSON $ map toTokenizedLine tokenizedLines)
, ("identifiers" , A.toJSON idInfoMap)
, ("occurrences" , A.toJSON $ idOccurrencesHashMap idOccMap)
, ("declarations" , A.toJSON declarations)
]
+toTokenizedLine
+ :: (Int, [(T.Text, (Int, Int), Maybe IdentifierOccurrence)]) -> TokenizedLine
+toTokenizedLine (lineNumber, tokens) = TokenizedLine lineNumber contents
+ where
+ contents = map
+ (\(content, (start, end), mbIdOcc) -> TokenizedContent
+ start
+ end
+ content
+ ( (\IdentifierOccurrence {..} -> TokenizedIdInfo
+ (maybe "" getInternalId internalIdFromRenamedSource)
+ (occurrenceLocationToText lineNumber start end)
+ (maybe "" getInternalId internalId)
+ )
+ <$> mbIdOcc
+ )
+ )
+ tokens
+
+data TokenizedLine = TokenizedLine
+ { lineNumber :: Int
+ , lineContents :: [TokenizedContent]
+ }
+ deriving (Show, Generic)
+instance A.ToJSON TokenizedLine
+
+data TokenizedContent = TokenizedContent
+ { start :: Int
+ , end :: Int
+ , content :: T.Text
+ , idInfo :: Maybe TokenizedIdInfo
+ }
+ deriving (Show, Generic)
+instance A.ToJSON TokenizedContent
+
+data TokenizedIdInfo = TokenizedIdInfo
+ { id :: T.Text
+ , occurrence :: T.Text
+ , identifier :: T.Text
+ }
+ deriving (Show, Generic)
+instance A.ToJSON TokenizedIdInfo
+
idOccurrencesHashMap
:: IM.IntMap [((Int, Int), IdentifierOccurrence)]
-> HM.HashMap T.Text IdentifierOccurrence