Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: the trailingWhitespace linter #920

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Batteries.lean
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import Batteries.Lean.System.IO
import Batteries.Lean.TagAttribute
import Batteries.Lean.Util.EnvSearch
import Batteries.Linter
import Batteries.Linter.TrailingWhitespace
import Batteries.Linter.UnnecessarySeqFocus
import Batteries.Linter.UnreachableTactic
import Batteries.Logic
Expand Down
1 change: 1 addition & 0 deletions Batteries/Linter.lean
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Batteries.Linter.TrailingWhitespace
import Batteries.Linter.UnreachableTactic
import Batteries.Linter.UnnecessarySeqFocus
48 changes: 48 additions & 0 deletions Batteries/Linter/TrailingWhitespace.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Lean.Elab.Command
import Lean.Linter.Util

/-!
# The "trailingWhitespace" linter

The "trailingWhitespace" linter emits a warning whenever a line ends with a space or a file
does not end with a line break.
-/

open Lean Elab

namespace Batteries.Linter

/--
The "trailingWhitespace" linter emits a warning whenever a line ends with a space or a file
does not end with a line break.
-/
register_option linter.trailingWhitespace : Bool := {
defValue := false
descr := "enable the trailingWhitespace linter"
}

namespace TrailingWhitespace

@[inherit_doc Batteries.Linter.linter.trailingWhitespace]
def trailingWhitespaceLinter : Linter where run := withSetOptionIn fun stx => do
unless Linter.getLinterValue linter.trailingWhitespace (← getOptions) do
return
if (← get).messages.hasErrors then
return
unless Parser.isTerminalCommand stx do return
let fm ← getFileMap
for lb in fm.positions do
let last : Substring := { str := fm.source, startPos := ⟨lb.1 - 2⟩, stopPos := ⟨lb.1 - 1⟩ }
if last.toString == " " then
Linter.logLint linter.trailingWhitespace (.ofRange ⟨⟨lb.1 - 2⟩, ⟨lb.1 - 1⟩⟩)
m!"Lines should not end with a space."
let (backBack, back) := (fm.positions.pop.back, fm.positions.back)
let rg : String.Range := ⟨back - ⟨1⟩, back⟩
if backBack != back then
Linter.logLint linter.trailingWhitespace (.ofRange rg) m!"Files should end with a line-break."

initialize addLinter trailingWhitespaceLinter

end TrailingWhitespace

end Batteries.Linter
18 changes: 18 additions & 0 deletions test/TrailingWhitespace.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Batteries.Linter.TrailingWhitespace

set_option linter.trailingWhitespace false

section
end
/--
warning: using 'exit' to interrupt Lean
---
warning: Lines should not end with a space.
note: this linter can be disabled with `set_option linter.trailingWhitespace false`
---
warning: Files should end with a line-break.
note: this linter can be disabled with `set_option linter.trailingWhitespace false`
-/
#guard_msgs in
set_option linter.trailingWhitespace true in
#exit