44
55import 'dart:async' show Future;
66
7- import 'dart:io' show File;
7+ import 'dart:io' show File, Platform, stdin, stdout ;
88
99import 'dart:typed_data' show Uint8List;
1010
1111import '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-
1613import 'package:_fe_analyzer_shared/src/scanner/token.dart'
1714 show Token, KeywordToken, BeginToken;
1815
1916import '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+
2121import '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 ("================" );
0 commit comments