@@ -74,9 +74,10 @@ import Data.Ord
7474import qualified Data.HashMap.Strict as HashMap
7575import qualified Data.Map.Strict as Map
7676import Data.Maybe
77- import qualified Data.Text.Rope as URope
77+ import qualified Data.Text.Lines as ULines
7878import Data.Text.Utf16.Rope ( Rope )
7979import qualified Data.Text.Utf16.Rope as Rope
80+ import qualified Data.Text.Utf16.Lines as Lines
8081import Data.Text.Prettyprint.Doc
8182import qualified Language.LSP.Types as J
8283import qualified Language.LSP.Types.Lens as J
@@ -367,15 +368,12 @@ data CodePointPosition =
367368-- We need the file itself because this requires translating between code points and code units.
368369codePointPositionToPosition :: VirtualFile -> CodePointPosition -> J. Position
369370codePointPositionToPosition 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
379377 in J. Position (fromIntegral cul) (fromIntegral cuc)
380378
381379-- | 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) =
384382-- We need the file itself because this requires translating between code unit and code points.
385383positionToCodePointPosition :: VirtualFile -> J. Position -> Maybe CodePointPosition
386384positionToCodePointPosition 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
394391 pure $ CodePointPosition (fromIntegral cpl) (fromIntegral cpc)
395392
396393-- ---------------------------------------------------------------------
0 commit comments