ligo/tools/lsp/squirrel/src/AST/Completion.hs

66 lines
1.5 KiB
Haskell
Raw Normal View History

2020-08-05 20:31:54 +04:00
module AST.Completion where
2020-08-07 14:27:07 +04:00
import Data.Function (on)
import Data.List (isSubsequenceOf, nubBy)
import Data.Maybe (listToMaybe)
2020-08-05 20:31:54 +04:00
import Data.Text (Text)
import qualified Data.Text as Text
import Duplo.Lattice
import Duplo.Pretty
import Duplo.Tree
2020-08-05 20:31:54 +04:00
import AST.Scope
import AST.Skeleton
2020-08-05 20:31:54 +04:00
import Product
import Range
2020-08-05 20:31:54 +04:00
2020-08-07 14:27:07 +04:00
data Completion = Completion
{ cName :: Text
, cType :: Text
, cDoc :: Text
}
deriving (Show)
2020-08-05 20:31:54 +04:00
complete
:: ( Eq (Product xs)
, Modifies (Product xs)
, Contains Range xs
, Contains [ScopedDecl] xs
, Contains (Maybe Category) xs
)
=> Range
-> LIGO (Product xs)
2020-08-07 14:27:07 +04:00
-> Maybe [Completion]
2020-08-05 20:31:54 +04:00
complete r tree = do
let l = spineTo (leq r . getElem) tree
word <- listToMaybe l
let scope = getElem (extract word)
let nameCat = getElem (extract word)
2020-08-05 20:31:54 +04:00
return
2020-08-07 14:27:07 +04:00
$ filter (isSubseqOf (ppToText word) . cName)
$ nubBy ((==) `on` cName)
$ map asCompletion
$ filter (fits nameCat . catFromType)
2020-08-05 20:31:54 +04:00
$ scope
2020-08-07 14:27:07 +04:00
asCompletion :: ScopedDecl -> Completion
asCompletion sd = Completion
{ cName = ppToText (_sdName sd)
, cType = ppToText (_sdType sd)
, cDoc = ppToText (fsep $ map pp $ _sdDoc sd)
}
2020-08-05 20:31:54 +04:00
isSubseqOf :: Text -> Text -> Bool
2020-08-07 14:27:07 +04:00
isSubseqOf l r =
-- traceShow (l, r, isSubsequenceOf (Text.unpack l) (Text.unpack r)) $
isSubsequenceOf (Text.unpack l) (Text.unpack r)
2020-08-05 20:31:54 +04:00
fits :: Maybe Category -> Category -> Bool
fits Nothing _ = True
fits (Just c) c' = c == c'
catFromType :: ScopedDecl -> Category
catFromType = maybe Variable (either (const Variable) (const Type)) . _sdType