Skip to content

Commit e82fd5a

Browse files
committed
[ new ] Merge & use command-line options passed from agda-mode
1 parent 0079a44 commit e82fd5a

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

src/Agda.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ import qualified Data.Aeson as JSON
6464
import Data.Maybe ( listToMaybe )
6565
import Data.Text ( pack )
6666
import GHC.Generics ( Generic )
67+
import Language.LSP.Server ( getConfig )
6768
import Monad
68-
import Options ( Options(optRawAgdaOptions) )
69+
import Options ( Config(configRawAgdaOptions)
70+
, Options(optRawAgdaOptions)
71+
)
6972

7073
getAgdaVersion :: String
7174
getAgdaVersion = versionWithCommitInfo
@@ -167,10 +170,16 @@ parseIOTCM raw = case listToMaybe $ reads raw of
167170
getCommandLineOptions
168171
:: (HasOptions m, MonadIO m) => ServerM m CommandLineOptions
169172
getCommandLineOptions = do
173+
-- command line options from ARGV
170174
argv <- asks (optRawAgdaOptions . envOptions)
175+
-- command line options from agda-mode
176+
config <- asks (configRawAgdaOptions . envConfig)
177+
-- concatenate both
178+
let merged = argv <> config
179+
171180
result <- runExceptT $ do
172181
(bs, opts) <- ExceptT $ runOptM $ parseBackendOptions builtinBackends
173-
argv
182+
merged
174183
defaultOptions
175184
return opts
176185
case result of

src/Monad.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
module Monad where
23

34
import Agda.IR
@@ -21,6 +22,9 @@ import Data.IORef ( IORef
2122
, writeIORef
2223
)
2324
import Data.Maybe ( isJust )
25+
import Language.LSP.Server ( MonadLsp
26+
, getConfig
27+
)
2428
import qualified Language.LSP.Types as LSP
2529
import Options
2630

@@ -29,19 +33,21 @@ import Options
2933
data Env = Env
3034
{ envOptions :: Options
3135
, envDevMode :: Bool
36+
, envConfig :: Config
3237
, envLogChan :: Chan Text
3338
, envCommandController :: CommandController
3439
, envResponseChan :: Chan Response
3540
, envResponseController :: ResponseController
3641
}
3742

38-
createInitEnv :: Options -> IO Env
43+
createInitEnv :: (MonadIO m, MonadLsp Config m) => Options -> m Env
3944
createInitEnv options =
4045
Env options (isJust (optViaTCP options))
41-
<$> newChan
42-
<*> CommandController.new
43-
<*> newChan
44-
<*> ResponseController.new
46+
<$> getConfig
47+
<*> liftIO newChan
48+
<*> liftIO CommandController.new
49+
<*> liftIO newChan
50+
<*> liftIO ResponseController.new
4551

4652
--------------------------------------------------------------------------------
4753

src/Options.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ usageMessage = usageInfo usage options ++ usageAboutAgdaOptions
3030

3131
-- | Command-line arguments
3232
data Options = Options
33-
{ optViaTCP :: Maybe Int
33+
{ optViaTCP :: Maybe Int
3434
, optRawAgdaOptions :: [String]
35-
, optHelp :: Bool
35+
, optHelp :: Bool
3636
}
3737

3838
defaultOptions :: Options
@@ -96,7 +96,7 @@ extractAgdaOpts argv =
9696

9797
--------------------------------------------------------------------------------
9898

99-
newtype Config = Config { commandLineOptions :: [String] }
99+
newtype Config = Config { configRawAgdaOptions :: [String] }
100100
deriving (Eq, Show, Generic)
101101

102102
instance FromJSON Config where

src/Server.hs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,35 @@ import Options
3636
--------------------------------------------------------------------------------
3737

3838
run :: Options -> IO Int
39-
run alsOptions = do
40-
env <- createInitEnv alsOptions
41-
switchboard <- Switchboard.new env
42-
case optViaTCP alsOptions of
39+
run options = do
40+
case optViaTCP options of
4341
Just port -> do
4442
void
4543
$ TCP.serve (TCP.Host "127.0.0.1") (show port)
4644
$ \(sock, _remoteAddr) -> do
47-
writeChan (envLogChan env) "[Server] connection established"
45+
-- writeChan (envLogChan env) "[Server] connection established"
4846
handle <- socketToHandle sock ReadWriteMode
49-
_ <- runServerWithHandles handle handle (serverDefn env switchboard)
47+
_ <- runServerWithHandles handle handle (serverDefn options)
5048
return ()
51-
Switchboard.destroy switchboard
49+
-- Switchboard.destroy switchboard
5250
return 0
5351
Nothing -> do
54-
runServer (serverDefn env switchboard)
52+
runServer (serverDefn options)
5553
where
56-
serverDefn :: Env -> Switchboard -> ServerDefinition Config
57-
serverDefn env switchboard = ServerDefinition
54+
serverDefn :: Options -> ServerDefinition Config
55+
serverDefn options = ServerDefinition
5856
{ defaultConfig = initConfig
5957
, onConfigurationChange = \old newRaw -> case JSON.fromJSON newRaw of
6058
JSON.Error s -> Left $ pack $ "Cannot parse server configuration: " <> s
6159
JSON.Success new -> Right new
6260
, doInitialize = \ctxEnv _req -> do
61+
env <- runLspT ctxEnv (createInitEnv options)
62+
switchboard <- Switchboard.new env
6363
Switchboard.setupLanguageContextEnv switchboard ctxEnv
64-
pure $ Right ctxEnv
64+
pure $ Right (ctxEnv, env)
6565
, staticHandlers = handlers
66-
, interpretHandler = \ctxEnv -> Iso (runLspT ctxEnv . runServerM env) liftIO
66+
, interpretHandler = \(ctxEnv, env) ->
67+
Iso (runLspT ctxEnv . runServerM env) liftIO
6768
, options = lspOptions
6869
}
6970

0 commit comments

Comments
 (0)