@@ -3,7 +3,7 @@ use itertools::Itertools;
3
3
use super :: lint:: Suggestion ;
4
4
use super :: { Lint , LintKind , Linter } ;
5
5
use crate :: document:: Document ;
6
- use crate :: TokenStringExt ;
6
+ use crate :: { Token , TokenKind , TokenStringExt } ;
7
7
8
8
#[ derive( Debug , Clone , Copy , Default ) ]
9
9
pub struct SentenceCapitalization ;
@@ -14,26 +14,45 @@ impl Linter for SentenceCapitalization {
14
14
fn lint ( & mut self , document : & Document ) -> Vec < Lint > {
15
15
let mut lints = Vec :: new ( ) ;
16
16
17
- for sentence in document. iter_sentences ( ) {
18
- if let Some ( first_word) = sentence. first_non_whitespace ( ) {
19
- if !first_word. kind . is_word ( ) {
17
+ for paragraph in document. iter_paragraphs ( ) {
18
+ // Allows short, label-like comments in code.
19
+ if paragraph. iter_sentences ( ) . count ( ) == 1 {
20
+ let only_sentence = paragraph. iter_sentences ( ) . next ( ) . unwrap ( ) ;
21
+
22
+ if !only_sentence
23
+ . iter_chunks ( )
24
+ . map ( |c| c. iter_words ( ) . count ( ) )
25
+ . any ( |c| c > 5 )
26
+ {
20
27
continue ;
21
28
}
29
+ }
30
+
31
+ for sentence in paragraph. iter_sentences ( ) {
32
+ if !is_full_sentence ( sentence) {
33
+ continue ;
34
+ }
35
+
36
+ if let Some ( first_word) = sentence. first_non_whitespace ( ) {
37
+ if !first_word. kind . is_word ( ) {
38
+ continue ;
39
+ }
22
40
23
- let letters = document. get_span_content ( first_word. span ) ;
24
-
25
- if let Some ( first_letter) = letters. first ( ) {
26
- if first_letter. is_alphabetic ( ) && !first_letter. is_uppercase ( ) {
27
- lints. push ( Lint {
28
- span : first_word. span . with_len ( 1 ) ,
29
- lint_kind : LintKind :: Capitalization ,
30
- suggestions : vec ! [ Suggestion :: ReplaceWith (
31
- first_letter. to_uppercase( ) . collect_vec( ) ,
32
- ) ] ,
33
- priority : 31 ,
34
- message : "This sentence does not start with a capital letter"
35
- . to_string ( ) ,
36
- } )
41
+ let letters = document. get_span_content ( first_word. span ) ;
42
+
43
+ if let Some ( first_letter) = letters. first ( ) {
44
+ if first_letter. is_alphabetic ( ) && !first_letter. is_uppercase ( ) {
45
+ lints. push ( Lint {
46
+ span : first_word. span . with_len ( 1 ) ,
47
+ lint_kind : LintKind :: Capitalization ,
48
+ suggestions : vec ! [ Suggestion :: ReplaceWith (
49
+ first_letter. to_uppercase( ) . collect_vec( ) ,
50
+ ) ] ,
51
+ priority : 31 ,
52
+ message : "This sentence does not start with a capital letter"
53
+ . to_string ( ) ,
54
+ } )
55
+ }
37
56
}
38
57
}
39
58
}
@@ -43,25 +62,52 @@ impl Linter for SentenceCapitalization {
43
62
}
44
63
}
45
64
65
+ fn is_full_sentence ( toks : & [ Token ] ) -> bool {
66
+ let mut has_noun = false ;
67
+ let mut has_verb = false ;
68
+
69
+ for tok in toks {
70
+ if let TokenKind :: Word ( metadata) = tok. kind {
71
+ if metadata. is_noun ( ) {
72
+ has_noun = true ;
73
+ }
74
+
75
+ if metadata. is_verb ( ) {
76
+ has_verb = true ;
77
+ }
78
+ }
79
+ }
80
+
81
+ has_noun && has_verb
82
+ }
83
+
46
84
#[ cfg( test) ]
47
85
mod tests {
48
86
use super :: super :: tests:: assert_lint_count;
49
87
use super :: SentenceCapitalization ;
50
88
51
89
#[ test]
52
90
fn catches_basic ( ) {
53
- assert_lint_count ( "there is no way." , SentenceCapitalization , 1 )
91
+ assert_lint_count (
92
+ "there is no way she is not guilty." ,
93
+ SentenceCapitalization ,
94
+ 1 ,
95
+ )
54
96
}
55
97
56
98
#[ test]
57
99
fn no_period ( ) {
58
- assert_lint_count ( "there is no way" , SentenceCapitalization , 1 )
100
+ assert_lint_count (
101
+ "there is no way she is not guilty" ,
102
+ SentenceCapitalization ,
103
+ 1 ,
104
+ )
59
105
}
60
106
61
107
#[ test]
62
108
fn two_sentence ( ) {
63
109
assert_lint_count (
64
- "i have complete conviction. she is guilty" ,
110
+ "i have complete conviction in this . she is absolutely guilty" ,
65
111
SentenceCapitalization ,
66
112
2 ,
67
113
)
@@ -111,4 +157,9 @@ mod tests {
111
157
1 ,
112
158
)
113
159
}
160
+
161
+ #[ test]
162
+ fn issue_228_allows_labels ( ) {
163
+ assert_lint_count ( "python lsp (fork of pyright)" , SentenceCapitalization , 0 )
164
+ }
114
165
}
0 commit comments