on master help sign in
Make the check for keywords a bit more accurate
210d7f9Evan Czaplicki
compiler/src/Reporting/Render/Code.hs+28−10
@@ -214,8 +214,9 @@ whatIsNext (Source sourceLines) row col =
214 Just line ->
214 Just line ->
215 let
215 let
216 chars = drop (fromIntegral col - 1) line
216 chars = drop (fromIntegral col - 1) line
217+ keywords = map Name.toChars (Set.toList reservedWords)
217 in
218 in
218− case List.find (\kwd -> startsWith kwd chars) (map Name.toChars (Set.toList reservedWords)) of
219+ case List.find (startsWithKeyword chars) keywords of
219 Just keyword ->
220 Just keyword ->
220 Keyword keyword
221 Keyword keyword
221
222
@@ -225,12 +226,12 @@ whatIsNext (Source sourceLines) row col =
225 Operator op
226 Operator op
226
227
227 []
228 []
228− | null chars -> Spaces
229+ | null chars -> Spaces
229− | startsWith " " chars -> Spaces
230+ | isNext ' ' chars -> Spaces
230− | startsWith ")" chars -> CloseParen
231+ | isNext ')' chars -> CloseParen
231− | startsWith "]" chars -> CloseSquare
232+ | isNext ']' chars -> CloseSquare
232− | startsWith "}" chars -> CloseCurly
233+ | isNext '}' chars -> CloseCurly
233− | otherwise -> Other
234+ | otherwise -> Other
234
235
235
236
236isSymbol :: Char -> Bool
237isSymbol :: Char -> Bool
@@ -238,6 +239,23 @@ isSymbol char =
238 IntSet.member (Char.ord char) binopCharSet
239 IntSet.member (Char.ord char) binopCharSet
239
240
240
241
241−startsWith :: [Char] -> [Char] -> Bool
242+isNext :: Char -> [Char] -> Bool
242−startsWith =
243+isNext expected chars =
244+ case chars of
245+ [] ->
246+ False
247+
248+ actual:_ ->
249+ actual == expected
250+
251+
252+startsWithKeyword :: [Char] -> [Char] -> Bool
253+startsWithKeyword restOfLine keyword =
243− List.isPrefixOf
254+ List.isPrefixOf keyword restOfLine
255+ &&
256+ case drop (length keyword) restOfLine of
257+ [] ->
258+ True
259+
260+ c:_ ->
261+ not (Char.isAlphaNum c || c == '_')