Skip to content

[WIP] Add auto-complete suggestions #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions server/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ import Control.Monad.Writer.Strict (runWriterT)
import qualified Data.Aeson as A
import Data.Aeson ((.=))
import Data.Bifunctor (first, second, bimap)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as Char8
import Data.Default (def)
import Data.Function (on)
import Data.Function (on, fix)
import qualified Data.IORef as IORef
import Data.List (nubBy)
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import Data.Time.Clock (UTCTime)
import qualified Data.Vector as V
import GHC.Generics (Generic)
import qualified Language.PureScript as P
import qualified Language.PureScript.CST as CST
Expand All @@ -47,6 +51,7 @@ import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.FilePath.Glob (glob)
import qualified System.IO as IO
import qualified System.Process as Process
import Web.Scotty
import qualified Web.Scotty as Scotty

Expand Down Expand Up @@ -113,6 +118,25 @@ buildMakeActions codegenRef =
outputPrimDocs :: Make.Make ()
outputPrimDocs = pure ()

-- mkCommand :: String -> String
-- mkCommand str = "\
-- \{ \"command\": \"complete\",\
-- \\"params\": {\
-- \\"filters\": [{\
-- \\"filter\": \"prefix\",\
-- \\"params\": {\
-- \\"search\": \"" <> str <> "\"\
-- \}\
-- \}],\
-- \\"options\": {\
-- \\"maxResults\": 20,\
-- \\"groupReexports\": true\
-- \}\
-- \}\
-- \}\
-- \"


server :: [P.ExternsFile] -> P.Env -> P.Environment -> Int -> IO ()
server externs initNamesEnv initEnv port = do
codegenRef <- IORef.newIORef Nothing
Expand Down Expand Up @@ -157,6 +181,35 @@ server externs initNamesEnv initEnv port = do
Scotty.json $ A.object [ "error" .= err ]
Right (warnings, comp) ->
Scotty.json $ A.object [ "js" .= comp, "warnings" .= warnings ]

get "/complete" $ do
query <- param "q"
Scotty.setHeader "Access-Control-Allow-Origin" "*"
Scotty.setHeader "Content-Type" "application/json"
let ideClient =
Process.createProcess_ "purs-ide-client"
(Process.proc "purs" ["ide", "client"])
{ Process.std_in = Process.CreatePipe
, Process.std_out = Process.CreatePipe
}
mkCommand q = A.encode $ A.object
[ "command" .= ("complete" :: Text)
, "params" .= A.object
[ "filters" .= A.Array
( V.fromList
[ A.object
[ "filter" .= ("prefix" :: Text)
, "params" .= A.object
[ "search" .= q ]
]
]
)
]
]
(Just handleIn, Just handleOut, _, _) <- liftIO ideClient
liftIO $ Char8.hPutStrLn handleIn (mkCommand (query :: Text))
result <- liftIO $ BS.hGetContents handleOut
Scotty.text (TL.fromStrict (T.decodeUtf8 result))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently works, but there might be some inefficiency here that I'm not spotting. Running purs ide client directly (talking to same purs ide server instance) is instantaneous, while hitting this endpoint takes around 2s, sometimes longer.

2022-02-06 16 00 04

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be a POST request with JSON constructed on the client, and passed directly to purs ide client

Copy link
Contributor Author

@pete-murphy pete-murphy Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's somehow much faster using POST 7496700, but I don't know if there's any security issues with just proxying directly to purs ide client / purs ide server (probably not since I think purs ide client will fail on malformed input?)


get "/search" $ do
query <- param "q"
Expand Down Expand Up @@ -240,4 +293,7 @@ main = do
pure (exts, namesEnv, env)
case e of
Left err -> print err >> exitFailure
Right (exts, namesEnv, env) -> server exts namesEnv env port
Right (exts, namesEnv, env) -> do
let ideServer = Process.proc "purs" ["ide", "server"]
Process.withCreateProcess ideServer $
\_ _ _ _ -> server exts namesEnv env port
14 changes: 7 additions & 7 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

packages:
- completed:
hackage: purescript-0.14.3@sha256:4b75604e86c335711e1b1f1f73ef381108d047c9277df1cdc71922ec7da7c181,18623
hackage: purescript-0.14.5@sha256:511f50e7f267b65e1f656cdff9f9665073496efdf4375a3a86aa68496dae7281,18623
pantry-tree:
size: 132222
sha256: e3957c9d2c96434fcf5e8f310b34f4ea696cc8e5f63e60958eae5c6e31aba4c6
size: 121096
sha256: 895dfc2fc938d99930f4ad17d1c475baf59efb4ad0c64519dfd09e482db38ff3
original:
hackage: purescript-0.14.3
hackage: purescript-0.14.5
- completed:
hackage: purescript-cst-0.3.0.0@sha256:369317d52737c4fa8c74a875283ed6cc0ef68e7c64db13d6e5bb7a7f72b76572,3861
hackage: purescript-cst-0.4.0.0@sha256:bfe7be3962e83b645a4a8cd1805f31de17db3d3456962e1a2d17016fe5d7f96d,3861
pantry-tree:
size: 3018
sha256: 38f94bcc121068215c6082bab39b6c555cadd3b88e786740c394a5c4b98c4100
sha256: 1681432cc9fa87bc6344e7c604f94a15a016af6e4b7c3519ee035f0f51d90c9c
original:
hackage: purescript-cst-0.3.0.0
hackage: purescript-cst-0.4.0.0
- completed:
hackage: language-javascript-0.7.0.0@sha256:3eab0262b8ac5621936a4beab6a0f97d0e00a63455a8b0e3ac1547b4088dae7d,3898
pantry-tree:
Expand Down
2 changes: 2 additions & 0 deletions trypurescript.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ executable trypurescript
http-types -any,
transformers -any,
mtl -any,
process -any,
text -any,
time -any,
vector -any,
warp -any
hs-source-dirs: server
main-is: Main.hs
Expand Down