From faa57eb57831a07a17a553a3ca230f55dd5260b7 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 19 Aug 2022 14:24:53 +1000 Subject: Adding tokenized lines to api --- src/HaskellCodeExplorer/Types.hs | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src') 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 -- cgit v1.2.3