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

Factor out the caret padding.

This commit is contained in:
Rob Rix 2018-08-02 16:46:05 -04:00
parent 66023c80de
commit b83a38ecaa

View File

@ -54,10 +54,11 @@ 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 (replicate (succ (posColumn spanStart + lineNumberDigits)) ' ') . withSGRCode colourize [SetColor Foreground Vivid Green] (showChar '^') . showChar '\n'
. showString (replicate (caretPaddingWidth + lineNumberDigits) ' ') . withSGRCode colourize [SetColor Foreground Vivid Green] (showChar '^') . showChar '\n'
where context = maybe "\n" (sourceBytes . sconcat) (nonEmpty [ fromUTF8 (pack (showLineNumber i)) <> fromUTF8 ": " <> 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)
withSGRCode :: Colourize -> [SGR] -> ShowS -> ShowS
withSGRCode useColour code content =