File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed
lsp-types/src/Language/LSP/Types Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change 1
- {-# LANGUAGE CPP #-}
1
+ {-# LANGUAGE CPP #-}
2
+ {-# LANGUAGE ScopedTypeVariables #-}
2
3
3
4
#if MIN_VERSION_filepath(1,4,100)
4
5
#define OS_PATH 1
@@ -35,8 +36,11 @@ import qualified Data.Text.Encoding as T
35
36
import System.FilePath
36
37
#endif
37
38
38
- import Control.Monad.Catch (MonadThrow )
39
+ import Control.Exception (try )
40
+ import Control.Monad.Catch (MonadThrow , SomeException ,
41
+ throwM )
39
42
import Data.ByteString.Short (ShortByteString )
43
+ import System.IO.Unsafe (unsafePerformIO )
40
44
41
45
type OsPathCompat =
42
46
#ifdef OS_PATH
@@ -69,14 +73,24 @@ fromShortByteString = T.unpack . T.decodeUtf8 . BS.fromShort
69
73
70
74
toFilePath :: MonadThrow m => OsPathCompat -> m FilePath
71
75
#ifdef OS_PATH
72
- toFilePath = decodeUtf
76
+ toFilePath = unsafePerformIO' . decodeFS
73
77
#else
74
78
toFilePath = pure
75
79
#endif
76
80
77
81
fromFilePath :: MonadThrow m => FilePath -> m OsPathCompat
78
82
#ifdef OS_PATH
79
- fromFilePath = encodeUtf
83
+ fromFilePath = unsafePerformIO' . encodeUtf
80
84
#else
81
85
fromFilePath = pure
82
86
#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
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ module Language.LSP.Types.Uri
14
14
, emptyNormalizedUri
15
15
, NormalizedFilePath
16
16
, toNormalizedFilePath
17
+ , unsafeToNormalizedFilePath
17
18
, fromNormalizedFilePath
18
19
, normalizedFilePathToUri
19
20
, uriToNormalizedFilePath
@@ -37,6 +38,7 @@ import Data.Maybe (fromJust)
37
38
import Data.Text (Text )
38
39
import qualified Data.Text as T
39
40
import GHC.Generics
41
+ import GHC.Stack (HasCallStack )
40
42
import Language.LSP.Types.OsPath.Compat (OsPathCompat )
41
43
import qualified Language.LSP.Types.OsPath.Compat as OsPath
42
44
import Network.URI hiding (authority )
@@ -216,6 +218,13 @@ toNormalizedFilePath fp = flip NormalizedFilePath (OsPath.toShortByteString nfp)
216
218
nfp = OsPath. normalise fp
217
219
nuri = internalNormalizedFilePathToUri nfp
218
220
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
+
219
228
fromNormalizedFilePath :: NormalizedFilePath -> OsPathCompat
220
229
fromNormalizedFilePath (NormalizedFilePath _ fp) = OsPath. fromShortByteString fp
221
230
You can’t perform that action at this time.
0 commit comments