Skip to content

Commit 3764113

Browse files
committed
add debugging option
1 parent deda7eb commit 3764113

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,19 @@ text objects to behave.
167167

168168
`ih`/`ah` will operate on `{!` and `!}`.
169169

170+
### Debugging
170171

172+
When debugging the cornelis plugin or creating an issue, set the `cornelis_debug` variable.
173+
174+
```viml
175+
let g:cornelis_debug = v:true
176+
```
177+
178+
```lua
179+
vim.g.cornelis_debug = true
180+
```
181+
182+
Cornelis will then output agda interaction information to `/tmp/agda.log`.
171183

172184
## Installation
173185

src/Cornelis/Agda.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module Cornelis.Agda where
55

66
import Control.Concurrent.Chan.Unagi (newChan, readChan, writeChan)
77
import Control.Lens
8-
import Control.Monad (forever, replicateM_, when)
8+
import Control.Monad (forever, replicateM_)
99
import Control.Monad.IO.Class
1010
import Control.Monad.State
11-
import Cornelis.Debug (reportExceptions)
11+
import Cornelis.Debug (reportExceptions, debugString)
1212
import Cornelis.InfoWin (buildInfoBuffer)
1313
import Cornelis.Types
1414
import Cornelis.Types.Agda
@@ -28,9 +28,8 @@ import System.Process
2828

2929
------------------------------------------------------------------------------
3030
-- | When true, dump out received JSON as it arrives.
31-
debugJson :: Bool
32-
debugJson = False
33-
31+
debugJson :: Neovim CornelisEnv Bool
32+
debugJson = asks $ cc_debug . ce_config
3433

3534
------------------------------------------------------------------------------
3635
-- | Create an 'Agda' environment for the given buffer. This spawns an
@@ -70,7 +69,7 @@ spawnAgda buffer = do
7069
Right res -> do
7170
case res of
7271
HighlightingInfo _ _ -> pure ()
73-
_ -> when debugJson $ vim_report_error $ T.pack $ show resp
72+
_ -> whenM debugJson $ debugString $ LT.unpack resp
7473
liftIO $ writeChan chan $ AgdaResp buffer res
7574

7675
void $ neovimAsync $ liftIO $ forever $ do

src/Cornelis/Config.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Cornelis.Config where
44

55
import Cornelis.Types
66
import Cornelis.Types.Agda (Rewrite(..))
7-
import Cornelis.Utils (objectToInt, objectToText)
7+
import Cornelis.Utils (objectToInt, objectToText, objectToBool)
88
import Data.Maybe (fromMaybe)
99
import qualified Data.Text as T
1010
import Neovim
@@ -40,5 +40,6 @@ getConfig
4040
<*> (fromMaybe Horizontal . (>>= (readSplitLocation . T.unpack <=< objectToText)) <$>
4141
getVarWithAlternatives ["cornelis_split_location", "cornelis_split_direction"])
4242
<*> (fromMaybe HeadNormal . (>>= (readRewrite . T.unpack <=< objectToText)) <$>
43-
(getVar "cornelis_rewrite_mode"))
43+
getVar "cornelis_rewrite_mode")
44+
<*> (fromMaybe False . (objectToBool =<<) <$> getVar "cornelis_debug")
4445

src/Cornelis/Debug.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ traceMX :: Show a => String -> a -> Neovim env ()
1515
traceMX herald a =
1616
vim_report_error $ "!!!" <> herald <> ": " <> show a
1717

18-
debug :: (Show a, MonadIO m) => a -> m ()
19-
debug x = liftIO $ go 100
18+
debugString :: MonadIO m => String -> m ()
19+
debugString s = liftIO $ go 100
2020
where
2121
go 0 = pure ()
2222
go n =
2323
catch
24-
(appendFile "/tmp/agda.log" (show x <> "\n"))
24+
(appendFile "/tmp/agda.log" (s <> "\n"))
2525
(\e -> if isAlreadyInUseError e then go (n-1 :: Int) else throw e)
26+
27+
28+
debug :: (Show a, MonadIO m) => a -> m ()
29+
debug = debugString . show

src/Cornelis/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ data CornelisConfig = CornelisConfig
116116
, cc_max_width :: Int64
117117
, cc_split_location :: SplitLocation
118118
, cc_rewrite_mode :: Rewrite
119+
, cc_debug :: Bool
119120
}
120121
deriving (Show, Generic)
121122

src/Cornelis/Utils.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ objectToText :: Object -> Maybe Text
3131
objectToText (ObjectString w) = Just $ decodeUtf8 w
3232
objectToText _ = Nothing
3333

34+
objectToBool :: Object -> Maybe Bool
35+
objectToBool (ObjectBool b) = Just b
36+
objectToBool o = objectToInt @Int o >>= \case
37+
0 -> Just False
38+
1 -> Just True
39+
_ -> Nothing
40+
3441
neovimAsync :: (MonadUnliftIO m) => m a -> m (Async a)
3542
neovimAsync m =
3643
withRunInIO $ \lower ->

0 commit comments

Comments
 (0)