Skip to content

Commit 3eaec8e

Browse files
author
kokobd
committed
add partial function for compatibility & use decodeFS
1 parent f675a60 commit 3eaec8e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lsp-types/src/Language/LSP/Types/OsPath/Compat.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{-# LANGUAGE CPP #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
23

34
#if MIN_VERSION_filepath(1,4,100)
45
#define OS_PATH 1
@@ -35,8 +36,11 @@ import qualified Data.Text.Encoding as T
3536
import System.FilePath
3637
#endif
3738

38-
import Control.Monad.Catch (MonadThrow)
39+
import Control.Exception (try)
40+
import Control.Monad.Catch (MonadThrow, SomeException,
41+
throwM)
3942
import Data.ByteString.Short (ShortByteString)
43+
import System.IO.Unsafe (unsafePerformIO)
4044

4145
type OsPathCompat =
4246
#ifdef OS_PATH
@@ -69,14 +73,24 @@ fromShortByteString = T.unpack . T.decodeUtf8 . BS.fromShort
6973

7074
toFilePath :: MonadThrow m => OsPathCompat -> m FilePath
7175
#ifdef OS_PATH
72-
toFilePath = decodeUtf
76+
toFilePath = unsafePerformIO' . decodeFS
7377
#else
7478
toFilePath = pure
7579
#endif
7680

7781
fromFilePath :: MonadThrow m => FilePath -> m OsPathCompat
7882
#ifdef OS_PATH
79-
fromFilePath = encodeUtf
83+
fromFilePath = unsafePerformIO' . encodeUtf
8084
#else
8185
fromFilePath = pure
8286
#endif
87+
88+
#ifdef OS_PATH
89+
unsafePerformIO' :: MonadThrow m => IO a -> m a
90+
unsafePerformIO' action =
91+
case fp of
92+
Left (e :: SomeException) -> throwM e
93+
Right fp' -> pure fp'
94+
where
95+
fp = unsafePerformIO . try $ action
96+
#endif

lsp-types/src/Language/LSP/Types/Uri.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Language.LSP.Types.Uri
1414
, emptyNormalizedUri
1515
, NormalizedFilePath
1616
, toNormalizedFilePath
17+
, unsafeToNormalizedFilePath
1718
, fromNormalizedFilePath
1819
, normalizedFilePathToUri
1920
, uriToNormalizedFilePath
@@ -37,6 +38,7 @@ import Data.Maybe (fromJust)
3738
import Data.Text (Text)
3839
import qualified Data.Text as T
3940
import GHC.Generics
41+
import GHC.Stack (HasCallStack)
4042
import Language.LSP.Types.OsPath.Compat (OsPathCompat)
4143
import qualified Language.LSP.Types.OsPath.Compat as OsPath
4244
import Network.URI hiding (authority)
@@ -216,6 +218,13 @@ toNormalizedFilePath fp = flip NormalizedFilePath (OsPath.toShortByteString nfp)
216218
nfp = OsPath.normalise fp
217219
nuri = internalNormalizedFilePathToUri nfp
218220

221+
{-# DEPRECATED unsafeToNormalizedFilePath "use toNormalizedFilePath instead" #-}
222+
unsafeToNormalizedFilePath :: HasCallStack => OsPathCompat -> NormalizedFilePath
223+
unsafeToNormalizedFilePath fp =
224+
case toNormalizedFilePath fp of
225+
Right nfp -> nfp
226+
Left exception -> error $ show exception
227+
219228
fromNormalizedFilePath :: NormalizedFilePath -> OsPathCompat
220229
fromNormalizedFilePath (NormalizedFilePath _ fp) = OsPath.fromShortByteString fp
221230

0 commit comments

Comments
 (0)