|
1 | 1 | {-# LANGUAGE CPP #-} |
2 | | -{-# LANGUAGE DeriveGeneric #-} |
3 | 2 |
|
4 | 3 | -- entry point of the LSP server |
5 | 4 |
|
6 | 5 | module Server |
7 | | - ( run |
8 | | - ) where |
| 6 | + ( run, |
| 7 | + ) |
| 8 | +where |
9 | 9 |
|
10 | 10 | import qualified Agda |
11 | | -import Control.Concurrent ( writeChan ) |
12 | | -import Control.Monad ( void ) |
13 | | -import Control.Monad.Reader ( MonadIO(liftIO) ) |
14 | | -import Data.Aeson ( FromJSON |
15 | | - , ToJSON |
16 | | - ) |
17 | | -import qualified Data.Aeson as JSON |
18 | | -import Data.Text ( pack ) |
19 | | -import GHC.IO.IOMode ( IOMode(ReadWriteMode) ) |
20 | | -import Language.LSP.Server hiding ( Options ) |
21 | | -import Language.LSP.Types hiding ( Options(..) |
22 | | - , TextDocumentSyncClientCapabilities(..) |
23 | | - ) |
24 | | -import Monad |
25 | | -import qualified Network.Simple.TCP as TCP |
26 | | -import Network.Socket ( socketToHandle ) |
| 11 | +import Control.Concurrent (writeChan) |
| 12 | +import Control.Monad (void) |
| 13 | +import Control.Monad.Reader (MonadIO (liftIO)) |
| 14 | +import Data.Aeson |
| 15 | + ( FromJSON, |
| 16 | + ToJSON, |
| 17 | + ) |
| 18 | +import qualified Data.Aeson as JSON |
| 19 | +import Data.Text (pack) |
| 20 | +import qualified Data.Text as T |
| 21 | +import GHC.IO.IOMode (IOMode (ReadWriteMode)) |
| 22 | +import Language.LSP.Server hiding (Options) |
| 23 | +import qualified Language.LSP.Server as LSP |
| 24 | +import Language.LSP.Types hiding |
| 25 | + ( Options (..), |
| 26 | + TextDocumentSyncClientCapabilities (..), |
| 27 | + ) |
| 28 | +import Monad |
| 29 | +import qualified Network.Simple.TCP as TCP |
| 30 | +import Network.Socket (socketToHandle) |
| 31 | +import Options |
| 32 | +import qualified Server.Handler as Handler |
| 33 | +import Switchboard (Switchboard) |
27 | 34 | import qualified Switchboard |
28 | | -import Switchboard ( Switchboard ) |
29 | | - |
30 | | -import qualified Server.Handler as Handler |
31 | | - |
32 | | -import qualified Language.LSP.Server as LSP |
33 | | -import Options |
34 | | - |
35 | 35 |
|
36 | 36 | -------------------------------------------------------------------------------- |
37 | 37 |
|
38 | 38 | run :: Options -> IO Int |
39 | 39 | run options = do |
40 | 40 | case optViaTCP options of |
41 | 41 | Just port -> do |
42 | | - void |
43 | | - $ TCP.serve (TCP.Host "127.0.0.1") (show port) |
44 | | - $ \(sock, _remoteAddr) -> do |
| 42 | + void $ |
| 43 | + TCP.serve (TCP.Host "127.0.0.1") (show port) $ |
| 44 | + \(sock, _remoteAddr) -> do |
45 | 45 | -- writeChan (envLogChan env) "[Server] connection established" |
46 | 46 | handle <- socketToHandle sock ReadWriteMode |
47 | | - _ <- runServerWithHandles |
| 47 | + _ <- runServerWithHandles |
48 | 48 | #if MIN_VERSION_lsp(1,5,0) |
49 | | - mempty mempty |
| 49 | + mempty mempty |
50 | 50 | #endif |
51 | | - handle handle (serverDefn options) |
| 51 | + handle handle (serverDefn options) |
52 | 52 | return () |
53 | 53 | -- Switchboard.destroy switchboard |
54 | 54 | return 0 |
55 | 55 | Nothing -> do |
56 | 56 | runServer (serverDefn options) |
57 | | - where |
58 | | - serverDefn :: Options -> ServerDefinition Config |
59 | | - serverDefn options = ServerDefinition |
60 | | - { defaultConfig = initConfig |
61 | | - , onConfigurationChange = \old newRaw -> case JSON.fromJSON newRaw of |
62 | | - JSON.Error s -> Left $ pack $ "Cannot parse server configuration: " <> s |
63 | | - JSON.Success new -> Right new |
64 | | - , doInitialize = \ctxEnv _req -> do |
65 | | - env <- runLspT ctxEnv (createInitEnv options) |
66 | | - switchboard <- Switchboard.new env |
67 | | - Switchboard.setupLanguageContextEnv switchboard ctxEnv |
68 | | - pure $ Right (ctxEnv, env) |
69 | | - , staticHandlers = handlers |
70 | | - , interpretHandler = \(ctxEnv, env) -> |
71 | | - Iso (runLspT ctxEnv . runServerM env) liftIO |
72 | | - , options = lspOptions |
73 | | - } |
| 57 | + where |
| 58 | + serverDefn :: Options -> ServerDefinition Config |
| 59 | + serverDefn options = |
| 60 | + ServerDefinition |
| 61 | + { defaultConfig = initConfig, |
| 62 | + onConfigurationChange = \old newRaw -> case JSON.fromJSON newRaw of |
| 63 | + JSON.Error s -> Left $ pack $ "Cannot parse server configuration: " <> s |
| 64 | + JSON.Success new -> Right new, |
| 65 | + doInitialize = \ctxEnv _req -> do |
| 66 | + env <- runLspT ctxEnv (createInitEnv options) |
| 67 | + switchboard <- Switchboard.new env |
| 68 | + Switchboard.setupLanguageContextEnv switchboard ctxEnv |
| 69 | + pure $ Right (ctxEnv, env), |
| 70 | + staticHandlers = handlers, |
| 71 | + interpretHandler = \(ctxEnv, env) -> |
| 72 | + Iso (runLspT ctxEnv . runServerM env) liftIO, |
| 73 | + options = lspOptions |
| 74 | + } |
74 | 75 |
|
75 | | - lspOptions :: LSP.Options |
76 | | - lspOptions = defaultOptions { textDocumentSync = Just syncOptions } |
| 76 | + lspOptions :: LSP.Options |
| 77 | + lspOptions = defaultOptions {textDocumentSync = Just syncOptions} |
77 | 78 |
|
78 | | - -- these `TextDocumentSyncOptions` are essential for receiving notifications from the client |
79 | | - syncOptions :: TextDocumentSyncOptions |
80 | | - syncOptions = TextDocumentSyncOptions { _openClose = Just True -- receive open and close notifications from the client |
81 | | - , _change = Just changeOptions -- receive change notifications from the client |
82 | | - , _willSave = Just False -- receive willSave notifications from the client |
83 | | - , _willSaveWaitUntil = Just False -- receive willSave notifications from the client |
84 | | - , _save = Just $ InR saveOptions |
85 | | - } |
| 79 | + -- these `TextDocumentSyncOptions` are essential for receiving notifications from the client |
| 80 | + syncOptions :: TextDocumentSyncOptions |
| 81 | + syncOptions = |
| 82 | + TextDocumentSyncOptions |
| 83 | + { _openClose = Just True, -- receive open and close notifications from the client |
| 84 | + _change = Just changeOptions, -- receive change notifications from the client |
| 85 | + _willSave = Just False, -- receive willSave notifications from the client |
| 86 | + _willSaveWaitUntil = Just False, -- receive willSave notifications from the client |
| 87 | + _save = Just $ InR saveOptions |
| 88 | + } |
86 | 89 |
|
87 | | - changeOptions :: TextDocumentSyncKind |
88 | | - changeOptions = TdSyncIncremental |
| 90 | + changeOptions :: TextDocumentSyncKind |
| 91 | + changeOptions = TdSyncIncremental |
89 | 92 |
|
90 | | - -- includes the document content on save, so that we don't have to read it from the disk |
91 | | - saveOptions :: SaveOptions |
92 | | - saveOptions = SaveOptions (Just True) |
| 93 | + -- includes the document content on save, so that we don't have to read it from the disk |
| 94 | + saveOptions :: SaveOptions |
| 95 | + saveOptions = SaveOptions (Just True) |
93 | 96 |
|
94 | 97 | -- handlers of the LSP server |
95 | 98 | handlers :: Handlers (ServerM (LspM Config)) |
96 | | -handlers = mconcat |
97 | | - [ -- custom methods, not part of LSP |
98 | | - requestHandler (SCustomMethod "agda") $ \req responder -> do |
99 | | - let RequestMessage _ _i _ params = req |
100 | | - response <- Agda.sendCommand params |
101 | | - responder $ Right response |
102 | | - , |
103 | | - -- hover provider |
104 | | - requestHandler STextDocumentHover $ \req responder -> do |
105 | | - let |
106 | | - RequestMessage _ _ _ (HoverParams (TextDocumentIdentifier uri) pos _workDone) |
107 | | - = req |
108 | | - result <- Handler.onHover uri pos |
109 | | - responder $ Right result |
110 | | - -- -- syntax highlighting |
111 | | - -- , requestHandler STextDocumentSemanticTokensFull $ \req responder -> do |
112 | | - -- result <- Handler.onHighlight (req ^. (params . textDocument . uri)) |
113 | | - -- responder result |
114 | | - ] |
| 99 | +handlers = |
| 100 | + mconcat |
| 101 | + [ -- custom methods, not part of LSP |
| 102 | + requestHandler (SCustomMethod "agda") $ \req responder -> do |
| 103 | + let RequestMessage _ _i _ params = req |
| 104 | + response <- Agda.sendCommand params |
| 105 | + responder $ Right response, |
| 106 | + -- hover provider |
| 107 | + requestHandler STextDocumentHover $ \req responder -> do |
| 108 | + let RequestMessage _ _ _ (HoverParams (TextDocumentIdentifier uri) pos _workDone) = |
| 109 | + req |
| 110 | + result <- Handler.onHover uri pos |
| 111 | + responder $ Right result, |
| 112 | + notificationHandler SInitialized $ \_not -> pure (), |
| 113 | + notificationHandler STextDocumentDidOpen $ \_not -> pure () |
| 114 | + -- -- syntax highlighting |
| 115 | + -- , requestHandler STextD_cumentSemanticTokensFull $ \req responder -> do |
| 116 | + -- result <- Handler.onHighlight (req ^. (params . textDocument . uri)) |
| 117 | + -- responder result |
| 118 | + ] |
0 commit comments