Skip to content

fix positionInRange #2625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ jobs:
# run the tests without parallelism to avoid running out of memory
run: cabal test ghcide --test-options="$TEST_OPTS" || cabal test ghcide --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test ghcide --test-options="$TEST_OPTS"

- if: matrix.test
name: Test hls-plugin-api
run: cabal test hls-plugin-api --test-options="$TEST_OPTS" || cabal test hls-plugin-api --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-plugin-api --test-options="$TEST_OPTS"

- if: matrix.test
name: Test func-test suite
env:
Expand Down
15 changes: 15 additions & 0 deletions hls-plugin-api/hls-plugin-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,18 @@ library
DataKinds
KindSignatures
TypeOperators

test-suite tests
type: exitcode-stdio-1.0
default-language: Haskell2010
hs-source-dirs: test
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
other-modules: Ide.PluginUtilsTest
build-depends:
base
, hls-plugin-api
, tasty
, tasty-hunit
, tasty-rerun
, lsp-types
9 changes: 3 additions & 6 deletions hls-plugin-api/src/Ide/PluginUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Ide.PluginUtils
allLspCmdIds',
installSigUsr1Handler,
subRange,
positionInRange,
usePropertyLsp,
response,
handleMaybe,
Expand Down Expand Up @@ -178,7 +179,7 @@ getClientConfig = getConfig
getPluginConfig :: MonadLsp Config m => PluginId -> m PluginConfig
getPluginConfig plugin = do
config <- getClientConfig
return $ configForPlugin config plugin
return $ configForPlugin config plugin

-- ---------------------------------------------------------------------

Expand Down Expand Up @@ -220,11 +221,7 @@ subRange smallRange range =
&& positionInRange (_end smallRange) range

positionInRange :: Position -> Range -> Bool
positionInRange (Position pl po) (Range (Position sl so) (Position el eo)) =
pl > sl && pl < el
|| pl == sl && pl == el && po >= so && po <= eo
|| pl == sl && po >= so
|| pl == el && po <= eo
positionInRange p (Range sp ep) = sp <= p && p <= ep

-- ---------------------------------------------------------------------

Expand Down
27 changes: 27 additions & 0 deletions hls-plugin-api/test/Ide/PluginUtilsTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Ide.PluginUtilsTest
( tests
) where

import Ide.PluginUtils (positionInRange)
import Language.LSP.Types (Position (Position), Range (Range))
import Test.Tasty
import Test.Tasty.HUnit

tests :: TestTree
tests = testGroup "PluginUtils"
[ positionInRangeTest
]

positionInRangeTest :: TestTree
positionInRangeTest = testGroup "positionInRange"
[ testCase "single line, after the end" $
positionInRange (Position 1 10) (Range (Position 1 1) (Position 1 3)) @?= False
, testCase "single line, before the begining" $
positionInRange (Position 1 0) (Range (Position 1 1) (Position 1 6)) @?= False
, testCase "single line, in range" $
positionInRange (Position 1 5) (Range (Position 1 1) (Position 1 6)) @?= True
, testCase "multiline, in range" $
positionInRange (Position 3 5) (Range (Position 1 1) (Position 5 6)) @?= True
, testCase "multiline, out of range" $
positionInRange (Position 3 5) (Range (Position 3 6) (Position 4 10)) @?= False
]
13 changes: 13 additions & 0 deletions hls-plugin-api/test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Main where

import qualified Ide.PluginUtilsTest as PluginUtilsTest
import Test.Tasty
import Test.Tasty.Ingredients.Rerun

main :: IO ()
main = defaultMainWithRerun tests

tests :: TestTree
tests = testGroup "Main"
[ PluginUtilsTest.tests
]