1
1
mirror of https://github.com/github/semantic.git synced 2024-12-29 18:06:14 +03:00

Perform fewer conversions back and forth from Source.

This commit is contained in:
Rob Rix 2018-08-02 16:57:00 -04:00
parent c219af9925
commit c5fc691abe

View File

@ -10,10 +10,9 @@ module Data.Error
import Prologue
import Data.ByteString (isSuffixOf)
import Data.ByteString.Char8 (pack, unpack)
import Data.ByteString.Char8 (unpack)
import Data.Ix (inRange)
import Data.List (intersperse)
import Data.List (intersperse, isSuffixOf)
import System.Console.ANSI
import Data.Blob
@ -53,10 +52,10 @@ formatError includeSource colourize blob@Blob{..} Error{..}
showExcerpt :: Colourize -> Span -> Blob -> ShowS
showExcerpt colourize Span{..} Blob{..}
= showString (unpack context) . (if "\n" `isSuffixOf` context then id else showChar '\n')
= showString context . (if "\n" `isSuffixOf` context then id else showChar '\n')
. showString (replicate (caretPaddingWidth + lineNumberDigits) ' ') . withSGRCode colourize [SetColor Foreground Vivid Green] (showChar '^') . showChar '\n'
where context = maybe "\n" (sourceBytes . sconcat) (nonEmpty contextLines)
contextLines = [ fromUTF8 (pack (showLineNumber i)) <> fromUTF8 ": " <> l | (i, l) <- zip [1..] (sourceLines blobSource), inRange (posLine spanStart - 2, posLine spanStart) i ]
where context = fold contextLines
contextLines = [ showLineNumber i <> ": " <> unpack (sourceBytes l) | (i, l) <- zip [1..] (sourceLines blobSource), inRange (posLine spanStart - 2, posLine spanStart) i ]
showLineNumber n = let s = show n in replicate (lineNumberDigits - length s) ' ' <> s
lineNumberDigits = succ (floor (logBase 10 (fromIntegral (posLine spanStart) :: Double)))
caretPaddingWidth = succ (posColumn spanStart)