fast word completion for functions names in the shell

This commit is contained in:
Krasimir Angelov
2023-03-02 10:28:00 +01:00
parent adc8a2fa29
commit f7ca8afa81
8 changed files with 87 additions and 2 deletions

View File

@@ -24,7 +24,7 @@ module PGF2 (-- * PGF
Cat,categories,categoryContext,categoryProbability,
-- ** Functions
Fun, functions, functionsByCat,
Fun, functions, functionsByPrefix, functionsByCat,
functionType, functionIsConstructor, functionProbability,
-- ** Expressions
@@ -1094,6 +1094,26 @@ functions p =
name <- peekText key
writeIORef ref $ (name : names)
-- | List of all functions whose names start with a given prefix
functionsByPrefix :: PGF -> String -> [Fun]
functionsByPrefix p prefix =
unsafePerformIO $ do
ref <- newIORef []
(withText prefix $ \c_prefix ->
allocaBytes (#size PgfItor) $ \itor ->
bracket (wrapItorCallback (getFunctions ref)) freeHaskellFunPtr $ \fptr ->
withForeignPtr (a_revision p) $ \c_revision -> do
(#poke PgfItor, fn) itor fptr
withPgfExn "functions" (pgf_iter_functions_by_prefix (a_db p) c_revision c_prefix itor)
fs <- readIORef ref
return (reverse fs))
where
getFunctions :: IORef [String] -> ItorCallback
getFunctions ref itor key _ exn = do
names <- readIORef ref
name <- peekText key
writeIORef ref $ (name : names)
-- | List of all functions defined in the abstract syntax
functionsByCat :: PGF -> Cat -> [Fun]
functionsByCat p cat =

View File

@@ -173,6 +173,8 @@ foreign import ccall pgf_category_prob :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText ->
foreign import ccall pgf_iter_functions :: Ptr PgfDB -> Ptr PGF -> Ptr PgfItor -> Ptr PgfExn -> IO ()
foreign import ccall pgf_iter_functions_by_prefix :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> Ptr PgfItor -> Ptr PgfExn -> IO ()
foreign import ccall pgf_iter_functions_by_cat :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> Ptr PgfItor -> Ptr PgfExn -> IO ()
foreign import ccall pgf_function_type :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> Ptr PgfUnmarshaller -> Ptr PgfExn -> IO (StablePtr Type)