Skip to content

Commit

Permalink
Use the same function for converting range types everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
anka-213 committed Sep 15, 2021
1 parent 9f4eb73 commit 08af9d5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lsp-tests/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import Language.LSP.Types
import Control.Monad.IO.Class (MonadIO(liftIO))
import qualified Data.Text as T
import qualified Language.LSP.Types as J
import L4LSP (handleUriErrs, LspError (ReadFileErr))
import L4LSP (handleUriErrs, LspError (ReadFileErr), realSRngToRange, posToPosition)
import Lexer (Err(Err))
import Annotation (SRng(DummySRng))
import Annotation (SRng(DummySRng), Pos (Pos), RealSRng)

main :: IO ()
main = defaultMain $ testGroup "Tests" [hoverTests, hoverTypeInfoTests, typeCheckerTests, unitTests]

-- | Takes 1-indexed range positions and converts them to 0-indexed range positions for LSP-server
mkRange' :: Int -> Int -> Int -> Int -> Range
mkRange' l1 c1 l2 c2 = mkRange (l1-1) (c1-1) (l2-1) (c2-1)
mkRange' l1 c1 l2 c2 = realSRngToRange $ RealSRng (Pos l1 c1) (Pos l2 c2)

mkPosition :: Int -> Int -> Position
mkPosition l c = Position (l-1) (c-1)
mkPosition l c = posToPosition $ Pos l c

hoverTests :: TestTree
hoverTests = testGroup "Hover tests"
Expand Down
2 changes: 1 addition & 1 deletion src/Annotation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Reason = String

data SRng =
RealSRng RealSRng
| DummySRng Reason
| DummySRng Reason
deriving (Eq, Ord, Show, Read, Data, Typeable)

data RealSRng = SRng
Expand Down
10 changes: 8 additions & 2 deletions src/L4LSP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,17 @@ posInRange (Position line col) srng = case sRngToRange srng of
Nothing -> False


-- | Convert l4 source ranges to lsp source ranges
-- | Convert 1-indexed l4 source ranges to 0-indexed lsp source ranges
sRngToRange :: SRng -> Maybe Range
sRngToRange (RealSRng (SRng (Pos l1 c1) (Pos l2 c2))) = Just $ Range (Position (l1-1) (c1-1)) (Position (l2-1) (c2-1))
sRngToRange (RealSRng srng) = Just $ realSRngToRange srng
sRngToRange (DummySRng _) = Nothing

realSRngToRange :: RealSRng -> Range
realSRngToRange (SRng p1 p2) = Range (posToPosition p1) (posToPosition p2)

posToPosition :: Pos -> Position
posToPosition (Pos l c) = Position (l-1) (c-1)

-- | Extract the range from an alex/happy error
errorRange :: Err -> Range
errorRange (Err s _)
Expand Down

0 comments on commit 08af9d5

Please sign in to comment.