Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ef7ae27

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
[CFE] Add interactive mode to spell checker
Change-Id: Ieec964b99d1c19c58e100b64f68e091178261172 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132644 Commit-Queue: Jens Johansen <jensj@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
1 parent eb271d9 commit ef7ae27

9 files changed

+125
-19
lines changed

pkg/front_end/test/spell_checking_list_blacklist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Comments can also be inline like 'correct # this is ok'.
77
# Note that at least one space before the hash is required.
88

9+
# Comments on a line by itself will be considered a header of the file and
10+
# automatic tools might move it to the top of the file.
11+
912
alread
1013
chnage
1114
clonable

pkg/front_end/test/spell_checking_list_code.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Comments can also be inline like 'correct # this is ok'.
77
# Note that at least one space before the hash is required.
88

9+
# Comments on a line by itself will be considered a header of the file and
10+
# automatic tools might move it to the top of the file.
11+
912
a+b
1013
abbreviate
1114
abcdef
@@ -503,7 +506,6 @@ json
503506
juxtaposition
504507
juxtapositions
505508
k
506-
k’s
507509
kallentu
508510
kernel
509511
kernel's
@@ -512,6 +514,7 @@ klass
512514
kmillikin
513515
kustermann
514516
kv
517+
k’s
515518
l
516519
lacks
517520
lang
@@ -564,6 +567,8 @@ md
564567
me
565568
merely
566569
meta
570+
method10a
571+
method10b
567572
method1a
568573
method1b
569574
method2a
@@ -586,8 +591,6 @@ method8a
586591
method8b
587592
method9a
588593
method9b
589-
method10a
590-
method10b
591594
mi
592595
migration
593596
mime
@@ -743,13 +746,13 @@ quick
743746
quiver
744747
quoted
745748
r
749+
r'$creation
746750
r'\f
747751
r'\r
748752
r'\s
749753
r'\t
750754
r'\u
751755
r'\v
752-
r'$creation
753756
r0i
754757
r1i
755758
r2i

pkg/front_end/test/spell_checking_list_common.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Comments can also be inline like 'correct # this is ok'.
77
# Note that at least one space before the hash is required.
88

9+
# Comments on a line by itself will be considered a header of the file and
10+
# automatic tools might move it to the top of the file.
11+
912
a
1013
abbreviations
1114
ability

pkg/front_end/test/spell_checking_list_messages.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Comments can also be inline like 'correct # this is ok'.
77
# Note that at least one space before the hash is required.
88

9+
# Comments on a line by itself will be considered a header of the file and
10+
# automatic tools might move it to the top of the file.
11+
912
argument(s)
1013
b
1114
c

pkg/front_end/test/spell_checking_list_tests.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Comments can also be inline like 'correct # this is ok'.
77
# Note that at least one space before the hash is required.
88

9+
# Comments on a line by itself will be considered a header of the file and
10+
# automatic tools might move it to the top of the file.
11+
912
a0x
1013
a1x
1114
a2x
@@ -111,6 +114,7 @@ dictionary
111114
differs
112115
dillfile
113116
dills
117+
dinteractive
114118
dirname
115119
disagree
116120
disallowed
@@ -201,6 +205,7 @@ increments
201205
inspect
202206
insufficient
203207
intact
208+
interactive
204209
internet
205210
interpolate
206211
inv

pkg/front_end/test/spelling_test_base.dart

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
import 'dart:async' show Future;
66

7-
import 'dart:io' show File;
7+
import 'dart:io' show File, Platform, stdin, stdout;
88

99
import 'dart:typed_data' show Uint8List;
1010

1111
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart' show ErrorToken;
1212

13-
import 'package:_fe_analyzer_shared/src/scanner/utf8_bytes_scanner.dart'
14-
show Utf8BytesScanner;
15-
1613
import 'package:_fe_analyzer_shared/src/scanner/token.dart'
1714
show Token, KeywordToken, BeginToken;
1815

1916
import 'package:_fe_analyzer_shared/src/scanner/token.dart';
2017

18+
import 'package:_fe_analyzer_shared/src/scanner/utf8_bytes_scanner.dart'
19+
show Utf8BytesScanner;
20+
2121
import 'package:front_end/src/fasta/command_line_reporting.dart'
2222
as command_line_reporting;
2323

@@ -33,6 +33,10 @@ abstract class SpellContext extends ChainContext {
3333
const SpellTest(),
3434
];
3535

36+
final bool interactive;
37+
38+
SpellContext({this.interactive});
39+
3640
// Override special handling of negative tests.
3741
@override
3842
Result processTestResult(
@@ -64,15 +68,91 @@ abstract class SpellContext extends ChainContext {
6468
print("================");
6569
print("The following word(s) were reported as unknown:");
6670
print("----------------");
67-
for (String s in reportedWords) {
68-
print("$s");
69-
}
70-
if (dictionaries.isNotEmpty) {
71-
print("----------------");
72-
print("If the word(s) are correctly spelled please add it to one of "
73-
"these files:");
71+
72+
spell.Dictionaries dictionaryToUse;
73+
if (dictionaries.contains(spell.Dictionaries.cfeTests)) {
74+
dictionaryToUse = spell.Dictionaries.cfeTests;
75+
} else if (dictionaries.contains(spell.Dictionaries.cfeMessages)) {
76+
dictionaryToUse = spell.Dictionaries.cfeMessages;
77+
} else if (dictionaries.contains(spell.Dictionaries.cfeCode)) {
78+
dictionaryToUse = spell.Dictionaries.cfeCode;
79+
} else {
7480
for (spell.Dictionaries dictionary in dictionaries) {
75-
print(" - ${spell.dictionaryToUri(dictionary)}");
81+
if (dictionaryToUse == null ||
82+
dictionary.index < dictionaryToUse.index) {
83+
dictionaryToUse = dictionary;
84+
}
85+
}
86+
}
87+
88+
if (interactive && dictionaryToUse != null) {
89+
List<String> addedWords = new List<String>();
90+
for (String s in reportedWords) {
91+
print("- $s");
92+
stdout.write("Do you want to add the word to the dictionary "
93+
"$dictionaryToUse (y/n)? ");
94+
String answer = stdin.readLineSync().trim().toLowerCase();
95+
bool add;
96+
switch (answer) {
97+
case "y":
98+
case "yes":
99+
case "true":
100+
add = true;
101+
break;
102+
case "n":
103+
case "no":
104+
case "false":
105+
add = false;
106+
break;
107+
default:
108+
throw "Didn't understand '$answer'";
109+
}
110+
if (add) {
111+
addedWords.add(s);
112+
}
113+
}
114+
if (addedWords.isNotEmpty) {
115+
File dictionaryFile =
116+
new File.fromUri(spell.dictionaryToUri(dictionaryToUse));
117+
List<String> lines = dictionaryFile.readAsLinesSync();
118+
List<String> header = new List<String>();
119+
List<String> sortThis = new List<String>();
120+
for (String line in lines) {
121+
if (line.startsWith("#")) {
122+
header.add(line);
123+
} else if (line.trim().isEmpty && sortThis.isEmpty) {
124+
header.add(line);
125+
} else if (line.trim().isNotEmpty) {
126+
sortThis.add(line);
127+
}
128+
}
129+
sortThis.addAll(addedWords);
130+
sortThis.sort();
131+
lines = new List<String>();
132+
lines.addAll(header);
133+
if (header.isEmpty || header.last.isNotEmpty) {
134+
lines.add("");
135+
}
136+
lines.addAll(sortThis);
137+
lines.add("");
138+
dictionaryFile.writeAsStringSync(lines.join("\n"));
139+
}
140+
} else {
141+
for (String s in reportedWords) {
142+
print("$s");
143+
}
144+
if (dictionaries.isNotEmpty) {
145+
print("----------------");
146+
print("If the word(s) are correctly spelled please add it to one of "
147+
"these files:");
148+
for (spell.Dictionaries dictionary in dictionaries) {
149+
print(" - ${spell.dictionaryToUri(dictionary)}");
150+
}
151+
152+
print("");
153+
print("To add words easily, try to run this script in interactive "
154+
"mode via the command");
155+
print("dart ${Platform.script.toFilePath()} -Dinteractive=true");
76156
}
77157
}
78158
print("================");

pkg/front_end/test/spelling_test_external_targets.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ main([List<String> arguments = const []]) =>
1818

1919
Future<SpellContext> createContext(
2020
Chain suite, Map<String, String> environment) async {
21-
return new SpellContextExternal();
21+
bool interactive = environment["interactive"] == "true";
22+
return new SpellContextExternal(interactive: interactive);
2223
}
2324

2425
class SpellContextExternal extends SpellContext {
26+
SpellContextExternal({bool interactive}) : super(interactive: interactive);
27+
2528
@override
2629
List<spell.Dictionaries> get dictionaries => const <spell.Dictionaries>[];
2730

pkg/front_end/test/spelling_test_not_src_suite.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ main([List<String> arguments = const []]) =>
1515

1616
Future<SpellContext> createContext(
1717
Chain suite, Map<String, String> environment) async {
18-
return new SpellContextTest();
18+
bool interactive = environment["interactive"] == "true";
19+
return new SpellContextTest(interactive: interactive);
1920
}
2021

2122
class SpellContextTest extends SpellContext {
23+
SpellContextTest({bool interactive}) : super(interactive: interactive);
24+
2225
@override
2326
List<spell.Dictionaries> get dictionaries => const <spell.Dictionaries>[
2427
spell.Dictionaries.common,

pkg/front_end/test/spelling_test_src_suite.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ main([List<String> arguments = const []]) =>
1515

1616
Future<SpellContext> createContext(
1717
Chain suite, Map<String, String> environment) async {
18-
return new SpellContextSource();
18+
bool interactive = environment["interactive"] == "true";
19+
return new SpellContextSource(interactive: interactive);
1920
}
2021

2122
class SpellContextSource extends SpellContext {
23+
SpellContextSource({bool interactive}) : super(interactive: interactive);
24+
2225
@override
2326
List<spell.Dictionaries> get dictionaries => const <spell.Dictionaries>[
2427
spell.Dictionaries.common,

0 commit comments

Comments
 (0)