Skip to content

Commit 786053e

Browse files
committed
Add warning on project failure
1 parent 15d6245 commit 786053e

9 files changed

Lines changed: 40 additions & 106 deletions

File tree

cpp-notebook-language-server.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ library
1818
Language.LSP.Notebook.DeclarationSifter
1919
Language.LSP.Test
2020
Language.LSP.Transformer
21-
Language.LSP.TransformTest
2221
Transform.ClientNot
2322
Transform.ClientNot.DidSave
2423
Transform.ClientReq

src/Language/LSP/Notebook/DeclarationSifter.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ instance Transformer DeclarationSifter where
3838

3939
getParams _ = DeclarationSifterParams "cling-parser" "__notebook_exec"
4040

41-
project :: MonadIO m => Params DeclarationSifter -> Doc -> m (Doc, DeclarationSifter)
41+
project :: MonadIO m => Params DeclarationSifter -> Doc -> m (Doc, DeclarationSifter, Either Text ())
4242
project params doc = do
4343
result <- parseCppCode params doc
4444
let originalLines = docToList doc
4545
numOrigLines = length originalLines
4646
case result of
47-
Left _err -> return (doc, mkIdentitySifter numOrigLines)
47+
Left err -> return (doc, mkIdentitySifter numOrigLines, Left (T.pack err))
4848
Right declarations -> do
4949
let (siftedIdxs, nonSiftedIdxs) = partitionIndices originalLines declarations
5050
siftedLines = map (originalLines !!) siftedIdxs
@@ -60,12 +60,12 @@ instance Transformer DeclarationSifter where
6060
bodyStart = numSifted + 1 -- after function header
6161
bodyEnd = numSifted + length nonSiftedLines -- before closing brace
6262
sifter = mkSifter numOrigLines siftedIdxs nonSiftedIdxs (Just (bodyStart, bodyEnd))
63-
return (listToDoc allLines, sifter)
63+
return (listToDoc allLines, sifter, Right ())
6464
else do
6565
-- No wrapper needed
6666
let allLines = siftedLines ++ nonSiftedLines
6767
sifter = mkSifter numOrigLines siftedIdxs nonSiftedIdxs Nothing
68-
return (listToDoc allLines, sifter)
68+
return (listToDoc allLines, sifter, Right ())
6969

7070
transformPosition :: Params DeclarationSifter -> DeclarationSifter -> Position -> Maybe Position
7171
transformPosition _ sifter (Position l c)

src/Language/LSP/TransformTest.hs

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Language/LSP/Transformer.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Control.Monad (foldM)
2323
import Control.Monad.IO.Class (MonadIO)
2424
import Data.Diff.Myers
2525
import qualified Data.Diff.Types as DT
26+
import Data.Either
2627
import Data.Kind
2728
import qualified Data.List as L
2829
import Data.Text (Text)
@@ -40,7 +41,7 @@ class Transformer a where
4041

4142
getParams :: a -> Params a
4243

43-
project :: MonadIO m => Params a -> Doc -> m (Doc, a)
44+
project :: MonadIO m => Params a -> Doc -> m (Doc, a, Either Text ())
4445

4546
handleDiffMulti :: MonadIO m => Params a -> Doc -> [TextDocumentContentChangeEvent] -> a -> m ([TextDocumentContentChangeEvent], a)
4647
handleDiffMulti params before changes tx = do
@@ -68,12 +69,15 @@ instance (Transformer a, Transformer b) => Transformer (a :> b) where
6869
type Params (a :> b) = Params a :> Params b
6970
getParams (x :> y) = getParams x :> getParams y
7071
project (xParams :> yParams) lines = do
71-
(lines', x) <- project xParams lines
72-
(lines'', y) <- project yParams lines'
73-
return (lines'', x :> y)
72+
(lines', x, eitherErr1) <- project xParams lines
73+
(lines'', y, eitherErr2) <- project yParams lines'
74+
let overallErr = case lefts [eitherErr1, eitherErr2] of
75+
[] -> Right ()
76+
xs -> Left (T.intercalate "; " xs)
77+
return (lines'', x :> y, overallErr)
7478
handleDiff (xParams :> yParams) before change (x :> y) = do
7579
(change', x') <- handleDiff xParams before change x
76-
(beforeTransformed, _) <- project @a xParams before
80+
(beforeTransformed, _, _eitherErr1) <- project @a xParams before
7781
(change'', y') <- handleDiffMulti yParams beforeTransformed change' y
7882
return (change'', x' :> y')
7983
transformPosition (xParams :> yParams) (x :> y) p = transformPosition xParams x p >>= transformPosition yParams y
@@ -82,9 +86,9 @@ instance (Transformer a, Transformer b) => Transformer (a :> b) where
8286
-- Default implementation uses diff.
8387
defaultHandleDiff :: forall a m. (Transformer a, MonadIO m) => Params a -> Doc -> TextDocumentContentChangeEvent -> a -> m ([TextDocumentContentChangeEvent], a)
8488
defaultHandleDiff params before change _transformer = do
85-
(before', _ :: a) <- project params before
89+
(before', _ :: a, _eitherErrBefore) <- project params before
8690
let after = applyChanges [change] before
87-
(after', transformer' :: a) <- project params after
91+
(after', transformer' :: a, _eitherErrAfter) <- project params after
8892
let change' = fmap repackChangeEvent $ diffTextsToChangeEventsConsolidate (Rope.toText before') (Rope.toText after')
8993
return (change', transformer')
9094
where

src/Transform/ClientNot.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ transformClientNot' sendExtraNotification SMethod_TextDocumentDidOpen params = w
5858
let t = params ^. (textDocument . text)
5959
let ls = Rope.fromText t
6060
let txParams = if isNotebook u then transformerParams else idTransformerParams
61-
(ls', transformer' :: CppNotebookTransformer) <- liftIO $ project txParams ls
61+
(ls', transformer' :: CppNotebookTransformer, eitherErr) <- liftIO $ project txParams ls
62+
63+
case eitherErr of
64+
Left err -> logWarnN [i|(#{u}) Project failed: #{err}|]
65+
Right () -> return ()
66+
6267
TransformerState {..} <- ask
6368
newUri <- addExtensionToUri ".cpp" u
6469
let referenceRegex = case uriToFilePath newUri of

test/Test/Transformer/Example1.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ spec :: TopSpec
5858
spec = describe "Example1" $ do
5959
it "produces expected output" $ do
6060
let inputDoc = listToDoc (T.splitOn "\n" testCode)
61-
(outputDoc, _ :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
61+
(outputDoc, _ :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
6262
T.intercalate "\n" (docToList outputDoc) `shouldBe` expectedFinalOutput
6363

6464
describe "position transformations" $ do
6565
it "transforms (7, 5) to (14, 7)" $ do
6666
let inputDoc = listToDoc (T.splitOn "\n" testCode)
67-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
67+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
6868
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
6969

7070
-- Line 7 (cout "Hello...") goes to wrapper body
@@ -74,15 +74,15 @@ spec = describe "Example1" $ do
7474

7575
it "untransforms (14, 7) to (7, 5)" $ do
7676
let inputDoc = listToDoc (T.splitOn "\n" testCode)
77-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
77+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
7878
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
7979

8080
Just pos <- return $ untransformPosition params sifter (Position 14 7)
8181
pos `shouldBe` Position 7 5
8282

8383
it "clamps column to 0 when untransforming from indentation area" $ do
8484
let inputDoc = listToDoc (T.splitOn "\n" testCode)
85-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
85+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
8686
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
8787

8888
Just pos <- return $ untransformPosition params sifter (Position 14 1)

test/Test/Transformer/Example2.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ spec :: TopSpec
2626
spec = describe "Example2" $ do
2727
it "produces expected output" $ do
2828
let inputDoc = listToDoc (T.splitOn "\n" testCode)
29-
(outputDoc, _ :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
29+
(outputDoc, _ :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
3030
T.intercalate "\n" (docToList outputDoc) `shouldBe` expectedFinalOutput
3131

3232
describe "position transformations" $ do
3333
it "transforms sqrt" $ do
3434
let inputDoc = listToDoc (T.splitOn "\n" testCode)
35-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
35+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
3636
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
3737

3838
-- sqrt is at position (1, 16) in input, should be at (2, 18) in output
@@ -42,7 +42,7 @@ spec = describe "Example2" $ do
4242

4343
it "untransforms sqrt" $ do
4444
let inputDoc = listToDoc (T.splitOn "\n" testCode)
45-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
45+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
4646
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
4747

4848
Just pos <- return $ untransformPosition params sifter (Position 2 18)

test/Test/Transformer/Example3.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ spec :: TopSpec
3838
spec = describe "Example3" $ do
3939
it "produces expected output" $ do
4040
let inputDoc = listToDoc (T.splitOn "\n" testCode)
41-
(outputDoc, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
41+
(outputDoc, sifter :: DeclarationSifter, eitherErr) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
42+
eitherErr `shouldBe` Right ()
4243
T.intercalate "\n" (docToList outputDoc) `shouldBe` expectedFinalOutput
4344

4445
-- forward[origLine] = outputLine
@@ -52,7 +53,7 @@ spec = describe "Example3" $ do
5253
describe "position transformations" $ do
5354
it "transforms #include (sifted, stays at line 0)" $ do
5455
let inputDoc = listToDoc (T.splitOn "\n" testCode)
55-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
56+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
5657
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
5758

5859
-- #include is at line 0 in input, stays at line 0 in output
@@ -61,15 +62,15 @@ spec = describe "Example3" $ do
6162

6263
it "untransforms #include" $ do
6364
let inputDoc = listToDoc (T.splitOn "\n" testCode)
64-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
65+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
6566
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
6667

6768
Just pos <- return $ untransformPosition params sifter (Position 0 0)
6869
pos `shouldBe` Position 0 0
6970

7071
it "transforms var (sifted but moved)" $ do
7172
let inputDoc = listToDoc (T.splitOn "\n" testCode)
72-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
73+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
7374
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
7475

7576
-- int x is at line 3 in input, should move to line 4 in output (include, using, class, func, VAR)
@@ -78,15 +79,15 @@ spec = describe "Example3" $ do
7879

7980
it "untransforms var" $ do
8081
let inputDoc = listToDoc (T.splitOn "\n" testCode)
81-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
82+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
8283
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
8384

8485
Just pos <- return $ untransformPosition params sifter (Position 4 0)
8586
pos `shouldBe` Position 3 0
8687

8788
it "transforms cout (executable, wrapped with indent)" $ do
8889
let inputDoc = listToDoc (T.splitOn "\n" testCode)
89-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
90+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
9091
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
9192

9293
-- First cout is at line 2 col 0 in input
@@ -97,15 +98,15 @@ spec = describe "Example3" $ do
9798

9899
it "untransforms cout" $ do
99100
let inputDoc = listToDoc (T.splitOn "\n" testCode)
100-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
101+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
101102
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
102103

103104
Just pos <- return $ untransformPosition params sifter (Position 6 2)
104105
pos `shouldBe` Position 2 0
105106

106107
it "transforms second cout" $ do
107108
let inputDoc = listToDoc (T.splitOn "\n" testCode)
108-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
109+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
109110
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
110111

111112
-- Second cout is at line 6 col 0 in input, should be at line 7 col 2 in output
@@ -114,7 +115,7 @@ spec = describe "Example3" $ do
114115

115116
it "untransforms second cout" $ do
116117
let inputDoc = listToDoc (T.splitOn "\n" testCode)
117-
(_, sifter :: DeclarationSifter) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
118+
(_, sifter :: DeclarationSifter, _) <- project (DeclarationSifterParams "cling-parser" "__notebook_exec") inputDoc
118119
let params = DeclarationSifterParams "cling-parser" "__notebook_exec"
119120

120121
Just pos <- return $ untransformPosition params sifter (Position 7 2)

test/TestLib/Core.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ testChange' extraProps params docLines change = do
2121
-- Expected un-projected document after the change
2222
let docLines' = applyChanges [change] docLines
2323

24-
(projectedBefore, transformer :: a) <- project params docLines
25-
(projectedAfter, reprojectedTransformer :: a) <- project params docLines'
24+
(projectedBefore, transformer :: a, _eitherErr) <- project params docLines
25+
(projectedAfter, reprojectedTransformer :: a, _eitherErr') <- project params docLines'
2626

2727
(changes, transformer') <- handleDiff params docLines change transformer
2828
let afterFromChange' = applyChanges changes projectedBefore

0 commit comments

Comments
 (0)