Skip to content

Commit e8298a2

Browse files
more tweaks
1 parent a6918ac commit e8298a2

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/swarm-lang/Swarm/Language/LSP.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE OverloadedStrings #-}
2-
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
32

43
-- |
54
-- SPDX-License-Identifier: BSD-3-Clause
@@ -14,10 +13,13 @@ import Control.Monad.IO.Class
1413
import Data.Int (Int32)
1514
import Data.Maybe (fromMaybe, mapMaybe)
1615
import Data.Text (Text)
16+
import Data.Text qualified as T
1717
import Data.Text.IO qualified as Text
1818
import Language.LSP.Diagnostics
1919
import Language.LSP.Protocol.Lens qualified as LSP
20+
import Language.LSP.Protocol.Message (TResponseError (TResponseError))
2021
import Language.LSP.Protocol.Message qualified as LSP
22+
import Language.LSP.Protocol.Types (ErrorCodes (..))
2123
import Language.LSP.Protocol.Types qualified as LSP
2224
import Language.LSP.Server
2325
import Language.LSP.VFS (VirtualFile (..), virtualFileText)
@@ -186,10 +188,12 @@ handlers =
186188
doc = uri ^. to LSP.toNormalizedUri
187189
pos = req ^. LSP.params . LSP.position
188190
mdoc <- getVirtualFile doc
189-
let (defs, path) = maybe ([], []) (D.findDefinition doc pos) mdoc
190-
debug $ from $ show path
191+
let defs = maybe D.Unsupported (D.findDefinition doc pos) mdoc
191192
case defs of
192-
[] -> responder . Right . LSP.InR . LSP.InR $ LSP.Null
193-
[def'] -> responder . Right . LSP.InL . LSP.Definition . LSP.InL $ LSP.Location uri def'
194-
defs' -> responder . Right . LSP.InL . LSP.Definition . LSP.InR $ LSP.Location uri <$> defs'
193+
D.Unsupported -> responder . Left $ TResponseError (LSP.InR ErrorCodes_MethodNotFound) "Unsupported find definition" Nothing
194+
D.PError e -> responder . Left $ TResponseError (LSP.InR ErrorCodes_ParseError) (T.pack $ show e) Nothing
195+
D.TError e -> responder . Left $ TResponseError (LSP.InR ErrorCodes_ParseError) (T.pack $ show e) Nothing
196+
D.NotFound -> responder . Right . LSP.InR . LSP.InR $ LSP.Null
197+
D.Found [def] -> responder . Right . LSP.InL . LSP.Definition . LSP.InL $ LSP.Location uri def
198+
D.Found defs' -> responder . Right . LSP.InL . LSP.Definition . LSP.InR $ LSP.Location uri <$> defs'
195199
]

src/swarm-lang/Swarm/Language/LSP/Definition.hs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,71 @@
22
-- SPDX-License-Identifier: BSD-3-Clause
33
module Swarm.Language.LSP.Definition (
44
findDefinition,
5+
DefinitionResult (..),
56
) where
67

78
import Data.List.NonEmpty qualified as NE
8-
import Data.Maybe (mapMaybe, maybeToList)
9+
import Data.Maybe (maybeToList)
910
import Data.Text.Utf16.Rope.Mixed qualified as R
11+
import Debug.Trace (traceShow)
1012
import Language.LSP.Protocol.Types qualified as J
1113
import Language.LSP.Protocol.Types qualified as LSP
1214
import Language.LSP.VFS (VirtualFile (VirtualFile), virtualFileText)
1315
import Swarm.Language.LSP.Position qualified as P
1416
import Swarm.Language.Parser (readTerm')
15-
import Swarm.Language.Parser.Core (defaultParserConfig)
17+
import Swarm.Language.Parser.Core (ParserError, defaultParserConfig)
1618
import Swarm.Language.Pipeline (processParsedTerm)
17-
import Swarm.Language.Syntax (LocVar, Located (..), SrcLoc, Syntax, Syntax' (Syntax'), Term' (..), Var)
19+
import Swarm.Language.Syntax (Located (..), SrcLoc, Syntax, Syntax' (Syntax'), Term' (..), Var)
1820
import Swarm.Language.TDVar (tdVarName)
19-
import Swarm.Language.Types (Polytype)
21+
import Swarm.Language.Typecheck (ContextualTypeErr)
22+
23+
data DefinitionResult = TError ContextualTypeErr | PError ParserError | Unsupported | NotFound | Found [J.Range]
2024

2125
findDefinition ::
2226
J.NormalizedUri ->
2327
J.Position ->
2428
VirtualFile ->
25-
([J.Range], [Syntax' Polytype])
29+
DefinitionResult
2630
findDefinition _ p vf@(VirtualFile _ _ myRope) =
2731
either
28-
(const ([], []))
29-
( \t -> case t of
30-
Nothing -> ([], [])
31-
Just t' -> findDef t'
32-
)
32+
PError
33+
(maybe Unsupported findDef)
3334
(readTerm' defaultParserConfig content)
3435
where
3536
content = virtualFileText vf
3637
absolutePos =
3738
R.charLength . fst $ R.charSplitAtPosition (P.lspToRopePosition p) myRope
3839

39-
findDef :: Syntax -> ([LSP.Range], [Syntax' Polytype])
40+
-- build a list from the syntax tree and starting from the position of the cursor (the bottom).
41+
-- search for the matching definition.
42+
findDef :: Syntax -> DefinitionResult
4043
findDef stx =
4144
case processParsedTerm stx of
42-
Left _e -> ([], [])
45+
Left e -> TError e
4346
Right pt -> do
4447
let path = P.pathToPosition pt $ fromIntegral absolutePos
45-
46-
-- The last element in the path is the thing we are looking for
47-
-- get it's name
4848
let usage = usageName $ NE.last path
49+
4950
case usage of
50-
Nothing -> ([], NE.toList path)
51+
Nothing -> Unsupported
5152
Just u -> do
52-
let pathTerms = concatMap syntaxVars $! (NE.drop 1 . NE.reverse $ path)
53-
(mapMaybe (maybeDefPosition u) pathTerms, NE.toList path)
53+
let pathTerms = concatMap syntaxVars (NE.drop 1 . NE.reverse $ path)
54+
traceShow pathTerms maybe NotFound Found (traverse (maybeDefPosition u) pathTerms)
5455

5556
-- take a syntax element that we want to find the defintion for and
5657
-- a possible syntax element that contains it's defintion
5758
-- if this is the matching definition return the position
5859
maybeDefPosition :: Var -> (SrcLoc, Var) -> Maybe LSP.Range
59-
maybeDefPosition name' (pos, name)
60+
maybeDefPosition name (pos, name')
6061
| name == name' = P.posToRange myRope pos
6162
| otherwise = Nothing
6263

6364
-- | find the name of the syntax element if it is a value level variable
6465
-- TODO if we want to support more jump to definitions we should extend this
6566
usageName :: Syntax' a -> Maybe Var
66-
usageName (Syntax' _ (TVar name) _ _) = Just name
67-
usageName _ = Nothing
67+
usageName (Syntax' _ t _ _) = case t of
68+
(TVar n) -> Just n
69+
_ -> Nothing
6870

6971
syntaxVars :: Syntax' a -> [(SrcLoc, Var)]
7072
syntaxVars (Syntax' _ t _ _) = case t of

0 commit comments

Comments
 (0)