@@ -74,9 +74,10 @@ import Data.Ord
74
74
import qualified Data.HashMap.Strict as HashMap
75
75
import qualified Data.Map.Strict as Map
76
76
import Data.Maybe
77
- import qualified Data.Text.Rope as URope
77
+ import qualified Data.Text.Lines as ULines
78
78
import Data.Text.Utf16.Rope ( Rope )
79
79
import qualified Data.Text.Utf16.Rope as Rope
80
+ import qualified Data.Text.Utf16.Lines as Lines
80
81
import Data.Text.Prettyprint.Doc
81
82
import qualified Language.LSP.Types as J
82
83
import qualified Language.LSP.Types.Lens as J
@@ -367,15 +368,12 @@ data CodePointPosition =
367
368
-- We need the file itself because this requires translating between code points and code units.
368
369
codePointPositionToPosition :: VirtualFile -> CodePointPosition -> J. Position
369
370
codePointPositionToPosition vFile (CodePointPosition cpl cpc) =
370
- let utf16Text = _file_text vFile
371
- -- Transcode to a code-point based rope
372
- utfText = URope. fromText $ Rope. toText utf16Text
373
- -- Split at the given position
374
- (utfPrefix, _) = URope. splitAtPosition (URope. Position (fromIntegral cpl) (fromIntegral cpc)) utfText
375
- -- Transcode the prefix to a code-unit based rope
376
- utf16Prefix = Rope. fromText $ URope. toText utfPrefix
377
- -- Get the length of the transcoded prefix
378
- (Rope. Position cul cuc) = Rope. lengthAsPosition utf16Prefix
371
+ let text = _file_text vFile
372
+ lines = Rope. toTextLines text
373
+ -- Split at the given position in *code points*
374
+ (prefix, _) = ULines. splitAtPosition (ULines. Position (fromIntegral cpl) (fromIntegral cpc)) lines
375
+ -- Get the length of the prefix in *code units*
376
+ (Lines. Position cul cuc) = Lines. lengthAsPosition prefix
379
377
in J. Position (fromIntegral cul) (fromIntegral cuc)
380
378
381
379
-- | Given a virtual file, translate a 'J.Position' in that file into a 'CodePointPosition' in that file.
@@ -384,13 +382,12 @@ codePointPositionToPosition vFile (CodePointPosition cpl cpc) =
384
382
-- We need the file itself because this requires translating between code unit and code points.
385
383
positionToCodePointPosition :: VirtualFile -> J. Position -> Maybe CodePointPosition
386
384
positionToCodePointPosition vFile (J. Position cul cuc) = do
387
- let utf16Text = _file_text vFile
388
- -- Split at the given location
389
- (utf16Prefix, _) <- Rope. splitAtPosition (Rope. Position (fromIntegral cul) (fromIntegral cuc)) utf16Text
390
- -- Transcode the preix to a code-point based rope
391
- let utfPrefix = URope. fromText $ Rope. toText utf16Prefix
392
- -- Get the length of the transcoded prefix
393
- (URope. Position cpl cpc) = URope. lengthAsPosition utfPrefix
385
+ let text = _file_text vFile
386
+ lines = Rope. toTextLines text
387
+ -- Split at the given location in *code units*
388
+ (prefix, _) <- Lines. splitAtPosition (Lines. Position (fromIntegral cul) (fromIntegral cuc)) lines
389
+ -- Get the length of the prefix in *code points*
390
+ let (ULines. Position cpl cpc) = ULines. lengthAsPosition prefix
394
391
pure $ CodePointPosition (fromIntegral cpl) (fromIntegral cpc)
395
392
396
393
-- ---------------------------------------------------------------------
0 commit comments