Skip to content

Commit 0437401

Browse files
author
rcartwright
committed
This revision contains minor refactoring of methods like isSourceFile. The behavior of unit tests appears unchanged. Our unit tests
still need some Scala updating. The following files were modified: M drjava/src/edu/rice/cs/drjava/model/DummyOpenDefDoc.java M drjava/src/edu/rice/cs/drjava/model/compiler/JavacCompiler.java M drjava/src/edu/rice/cs/drjava/model/AbstractGlobalModel.java M drjava/src/edu/rice/cs/drjava/model/DrJavaFileUtils.java M drjava/src/edu/rice/cs/drjava/model/OpenDefinitionsDocument.java M drjava/src/edu/rice/cs/drjava/model/definitions/DefinitionsDocumentTest.java M drjava/src/edu/rice/cs/drjava/model/definitions/DefinitionsDocument.java M drjava/src/edu/rice/cs/drjava/model/definitions/reducedmodel/ReducedModelState.java M drjava/src/edu/rice/cs/drjava/model/junit/DefaultJUnitModel.java git-svn-id: file:///tmp/test-svn/branches/drscala@5520 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 9286ddc commit 0437401

File tree

9 files changed

+99
-90
lines changed

9 files changed

+99
-90
lines changed

drjava/src/edu/rice/cs/drjava/model/AbstractGlobalModel.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ public File getWorkingDirectory() {
10161016

10171017
// if we can't get the source root of the current document, use the first document
10181018
Iterable<File> roots = getSourceRootSet();
1019-
if (!IterUtil.isEmpty(roots)) { return IterUtil.first(roots); }
1019+
if (! IterUtil.isEmpty(roots)) { return IterUtil.first(roots); }
10201020
else {
10211021
// use the last directory saved to the configuration
10221022
if (DrJava.getConfig().getSetting(STICKY_INTERACTIONS_DIRECTORY)) {
@@ -2382,7 +2382,7 @@ public List<OpenDefinitionsDocument> getOutOfSyncDocuments(List<OpenDefinitionsD
23822382
// "interface" or "enum". If the file doesn't, we presume it is empty and doesn't generate
23832383
// a class file; therefore, it cannot ever be out of sync.
23842384
try {
2385-
boolean b = doc.containsClassOrInterfaceOrEnum();
2385+
boolean b = doc.containsSource();
23862386
if (b) outOfSync.add(doc);
23872387
}
23882388
catch(BadLocationException e) {
@@ -2555,7 +2555,7 @@ public Iterable<File> getSourceRootSet() {
25552555
try {
25562556
if (! doc.isUntitled()) {
25572557
File root = doc.getSourceRoot();
2558-
if (root != null) roots.add(root); // Can't create duplicate entries in a Set
2558+
if (root != null && ! roots.contains(root)) roots.add(root); // Can't create duplicate entries in a Set
25592559
}
25602560
}
25612561
catch (InvalidPackageException e) {
@@ -3488,20 +3488,22 @@ private File _locateClassFile() {
34883488

34893489
// _log.log("build directory = " + getBuildDirectory());
34903490

3491-
if (getBuildDirectory() != FileOps.NULL_FILE) roots.add(getBuildDirectory());
3491+
File buildDir = getBuildDirectory();
3492+
3493+
if (buildDir != FileOps.NULL_FILE && ! roots.contain(buildDir) roots.add(buildDir);
34923494

34933495
// Add the current document to the beginning of the roots list
34943496
try {
34953497
File root = getSourceRoot();
34963498
// _log.log("Directory " + root + " added to list of source roots");
3497-
roots.add(root);
3499+
if (! roots.contains(root)) roots.add(root);
34983500
}
34993501
catch (InvalidPackageException ipe) {
35003502
try {
35013503
// _log.log(this + " has no source root, using parent directory instead");
35023504
File root = getFile().getParentFile();
3503-
if (root != FileOps.NULL_FILE) {
3504-
roots.add(root);
3505+
if (root != FileOps.NULL_FILE && ! roots.contains(root)) {
3506+
if (! roots.contains(root)) roots.add(root);
35053507
// _log.log("Added parent directory " + root + " to list of source roots");
35063508
}
35073509
}
@@ -3510,7 +3512,7 @@ private File _locateClassFile() {
35103512
// Moved, but we'll add the old file to the set anyway
35113513
_log.log("File for " + this + "has moved; adding parent directory to list of roots");
35123514
File root = fme.getFile().getParentFile();
3513-
if (root != FileOps.NULL_FILE) roots.add(root);
3515+
if (root != FileOps.NULL_FILE && ! roots.contains(root)) roots.add(root);
35143516
}
35153517
}
35163518

@@ -4014,8 +4016,8 @@ public int getOffsetOfLine(int line) {
40144016

40154017
/** Returns true if one of the words 'class', 'interface' or 'enum' is found
40164018
* in non-comment text. */
4017-
public boolean containsClassOrInterfaceOrEnum() throws BadLocationException {
4018-
return getDocument().containsClassOrInterfaceOrEnum();
4019+
public boolean containsSource() throws BadLocationException {
4020+
return getDocument().containsSource();
40194021
}
40204022
} /* End of ConcreteOpenDefDoc */
40214023

drjava/src/edu/rice/cs/drjava/model/DrJavaFileUtils.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ public static String getSuggestedFileExtension() {
7575
public static boolean isSourceFile(String fileName) {
7676
return fileName.endsWith(OptionConstants.JAVA_FILE_EXTENSION)
7777
|| fileName.endsWith(OptionConstants.SCALA_FILE_EXTENSION);
78-
// || fileName.endsWith(OptionConstants.DJ_FILE_EXTENSION)
79-
// || fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)
80-
// || fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)
81-
// || fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION);
8278
}
8379

8480
/** @return true if the file is a Java or language level file. */
@@ -90,19 +86,19 @@ public static boolean isSourceFile(File f) {
9086

9187
// Java language levels is disabled
9288

93-
/** .pjt --> true
94-
* otherwise false
95-
* @return true if the file is an old project file. */
96-
public static boolean isOldProjectFile(String fileName) {
97-
return fileName.endsWith(OptionConstants.OLD_PROJECT_FILE_EXTENSION);
98-
}
99-
100-
/** @return true if the file is an old project file. */
101-
public static boolean isOldProjectFile(File f) {
102-
File canonicalFile = IOUtil.attemptCanonicalFile(f);
103-
String fileName = canonicalFile.getPath();
104-
return isOldProjectFile(fileName);
105-
}
89+
// /** .pjt --> true
90+
// * otherwise false
91+
// * @return true if the file is an old project file. */
92+
// public static boolean isOldProjectFile(String fileName) {
93+
// return fileName.endsWith(OptionConstants.OLD_PROJECT_FILE_EXTENSION);
94+
// }
95+
//
96+
// /** @return true if the file is an old project file. */
97+
// public static boolean isOldProjectFile(File f) {
98+
// File canonicalFile = IOUtil.attemptCanonicalFile(f);
99+
// String fileName = canonicalFile.getPath();
100+
// return isOldProjectFile(fileName);
101+
// }
106102

107103
/** .pjt --> true
108104
* .drjava --> true
@@ -176,5 +172,10 @@ public static String getExtension(String fileName) {
176172
}
177173
return fileName.substring(lastDotIndex);
178174
}
175+
176+
/** Adds element to list provided list does not already contain element. */
177+
public static <T> ArrayList<T> addUnique(ArrayList<T> list, T element) {
178+
return list.contains(element) ? list : list.add(element);
179+
}
179180

180181
}

drjava/src/edu/rice/cs/drjava/model/DummyOpenDefDoc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public ArrayList<HighlightStatus> getHighlightStatus(int start, int end) {
447447
/** @return the caret position as set by the view. */
448448
public int getCaretPosition() { throw new UnsupportedOperationException("Dummy method"); }
449449

450-
public boolean containsClassOrInterfaceOrEnum() throws BadLocationException {
450+
public boolean containsSource() throws BadLocationException {
451451
throw new UnsupportedOperationException("Dummy method");
452452
}
453453

drjava/src/edu/rice/cs/drjava/model/OpenDefinitionsDocument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public interface OpenDefinitionsDocument extends DJDocument, Finalizable<Definit
395395

396396
/** Returns true if one of the words 'class', 'interface' or 'enum' is found
397397
* in non-comment text. */
398-
public boolean containsClassOrInterfaceOrEnum() throws BadLocationException;
398+
public boolean containsSource() throws BadLocationException;
399399

400400
/** Update the syntax highlighting for the file type. */
401401
public void updateSyntaxHighlighting();

drjava/src/edu/rice/cs/drjava/model/compiler/JavacCompiler.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,11 @@ protected static String _transformCommand(String s, String command) {
342342
protected static String _deleteSemiColon(String s) { return s.substring(0, s.length() - 1); }
343343

344344
/** .java --> true
345-
* .dj --> true
346-
* .dj0 --> true
347-
* .dj1 --> true
348-
* .dj2 --> true
349345
* otherwise false
350346
* @return true if the specified file is a source file for this compiler. */
351347
public boolean isSourceFileForThisCompiler(File f) {
352-
// by default, use DrJavaFileUtils.isSourceFile
353-
return DrJavaFileUtils.isSourceFile(f);
348+
String fileName = f.getName();
349+
return fileName.endsWith(OptionConstants.JAVA_FILE_EXTENSION)
354350
}
355351

356352
/** Return the set of source file extensions that this compiler supports.

drjava/src/edu/rice/cs/drjava/model/definitions/DefinitionsDocument.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,48 +1383,57 @@ protected void finalize() {
13831383

13841384
public String toString() { return "ddoc for " + _odd; }
13851385

1386-
/** Returns true if one of the words 'class', 'interface' or 'enum' is found
1387-
* in non-comment text. */
1388-
public boolean containsClassOrInterfaceOrEnum() throws BadLocationException {
1386+
/* Identical to subject.indexOf(pattern, start) except subject.length() is returned instead of -1 when pattern is
1387+
* not found. */
1388+
private static int myIndexOf(String subject, String pattern, int start) {
1389+
int matchIndex = subject.indexOf(pattern, start);
1390+
return (matchIndex < 0) ? subject.length() : matchIndex;
1391+
}
1392+
1393+
/** Returns true if one of the words 'class', 'interface', 'object', or 'enum', is found in non-comment text. */
1394+
public boolean containsSource() throws BadLocationException {
13891395

13901396
/* */ assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
1391-
int i, j;
1397+
int i, j, k, l;
1398+
int minPos;
13921399
int reducedPos = 0;
13931400

13941401
final String text = getText();
1402+
final int length = text.length();
13951403
final int origLocation = _currentLocation;
13961404
try {
1397-
// Move reduced model to beginning of file
1405+
// Move reduced model to beginning of file (reducedPos = 0)
13981406
_reduced.move(-origLocation);
13991407

1400-
// Walk forward from specificed position
1401-
i = text.indexOf("class", reducedPos);
1402-
j = text.indexOf("interface", reducedPos);
1403-
if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
1404-
j = text.indexOf("enum", reducedPos);
1405-
if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
1406-
while (i > - 1) {
1408+
// Find positions of critical keywords
1409+
i = myIndexOf(text, "class", reducedPos);
1410+
j = myIndexOf(text, "interface", reducedPos);
1411+
k = myIndexOf(text, "object", reducedPos);
1412+
l = myIndexOf(text, "enum", reducedPos);
1413+
1414+
minPos = Math.min(i, Math.min(j, Math.min(k, l)));
1415+
1416+
while (minPos < length) {
14071417
// Move reduced model to walker's location
1408-
_reduced.move(i - reducedPos); // reduced model points to i
1409-
reducedPos = i; // reduced model points to reducedPos
1418+
_reduced.move(minPos - reducedPos); // reduced model points to minPos
1419+
reducedPos = minPos; // update reducedPos; reduced model now also points to reducedPos
14101420

14111421
// Check if matching keyword should be ignored because it is within a comment, or quotes
14121422
ReducedModelState state = _reduced.getStateAtCurrent();
1413-
if (!state.equals(FREE) || _isStartOfComment(text, i) || ((i > 0) && _isStartOfComment(text, i - 1))) {
1414-
i = text.indexOf("class", reducedPos+1);
1415-
j = text.indexOf("interface", reducedPos+1);
1416-
if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
1417-
j = text.indexOf("enum", reducedPos+1);
1418-
if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
1419-
continue; // ignore match
1420-
}
1421-
else {
1422-
return true; // found match
1423-
}
1424-
}
1423+
1424+
if (state.equals(FREE)) return true; // keyword cannot be inside comment
1425+
1426+
i = myIndexOf(text, "class", reducedPos + 1);
1427+
j = myIndexOf(text, "interface", reducedPos + 1);
1428+
k = myIndexOf(text, "object", reducedPos + 1); // primary keyword in some Scala source files
1429+
l = myIndexOf(text, "enum", reducedPos + 1);
1430+
1431+
minPos = Math.min(i, Math.min(j, Math.min(k, l)));
1432+
}
14251433

14261434
return false;
14271435
}
1436+
14281437
finally {
14291438
_reduced.move(origLocation - reducedPos); // Restore the state of the reduced model;
14301439
}

drjava/src/edu/rice/cs/drjava/model/definitions/DefinitionsDocumentTest.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,102 +2436,102 @@ public void testgetMainClassName() throws BadLocationException, ClassNameNotFoun
24362436
_doc.remove(0, EIC_TEXT.length());
24372437
}
24382438

2439-
/** Test containsClassOrInterfaceOrEnum. */
2439+
/** Test containsSource. */
24402440
public void testContainsClassOrInterfaceOrEnum() throws BadLocationException {
24412441
_doc.insertString(0, "class", null);
2442-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2442+
assertTrue(_doc.containsSource());
24432443
_doc.remove(0, _doc.getText().length());
24442444

24452445
_doc.insertString(0, "interface", null);
2446-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2446+
assertTrue(_doc.containsSource());
24472447
_doc.remove(0, _doc.getText().length());
24482448

24492449
_doc.insertString(0, "enum", null);
2450-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2450+
assertTrue(_doc.containsSource());
24512451
_doc.remove(0, _doc.getText().length());
24522452

24532453
_doc.insertString(0, "foo", null);
2454-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2454+
assertFalse(_doc.containsSource());
24552455
_doc.remove(0, _doc.getText().length());
24562456

24572457
_doc.insertString(0, "//class", null);
2458-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2458+
assertFalse(_doc.containsSource());
24592459
_doc.remove(0, _doc.getText().length());
24602460

24612461
_doc.insertString(0, "//interface", null);
2462-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2462+
assertFalse(_doc.containsSource());
24632463
_doc.remove(0, _doc.getText().length());
24642464

24652465
_doc.insertString(0, "//enum", null);
2466-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2466+
assertFalse(_doc.containsSource());
24672467
_doc.remove(0, _doc.getText().length());
24682468

24692469
_doc.insertString(0, "//foo", null);
2470-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2470+
assertFalse(_doc.containsSource());
24712471
_doc.remove(0, _doc.getText().length());
24722472

24732473
_doc.insertString(0, "/*class*/", null);
2474-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2474+
assertFalse(_doc.containsSource());
24752475
_doc.remove(0, _doc.getText().length());
24762476

24772477
_doc.insertString(0, "/*interface*/", null);
2478-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2478+
assertFalse(_doc.containsSource());
24792479
_doc.remove(0, _doc.getText().length());
24802480

24812481
_doc.insertString(0, "/*enum*/", null);
2482-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2482+
assertFalse(_doc.containsSource());
24832483
_doc.remove(0, _doc.getText().length());
24842484

24852485
_doc.insertString(0, "/*foo*/", null);
2486-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2486+
assertFalse(_doc.containsSource());
24872487
_doc.remove(0, _doc.getText().length());
24882488

24892489
_doc.insertString(0, "\"class\"", null);
2490-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2490+
assertFalse(_doc.containsSource());
24912491
_doc.remove(0, _doc.getText().length());
24922492

24932493
_doc.insertString(0, "\"interface\"", null);
2494-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2494+
assertFalse(_doc.containsSource());
24952495
_doc.remove(0, _doc.getText().length());
24962496

24972497
_doc.insertString(0, "\"enum\"", null);
2498-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2498+
assertFalse(_doc.containsSource());
24992499
_doc.remove(0, _doc.getText().length());
25002500

25012501
_doc.insertString(0, "\"foo\"", null);
2502-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2502+
assertFalse(_doc.containsSource());
25032503
_doc.remove(0, _doc.getText().length());
25042504

25052505
_doc.insertString(0, "/*class*/class", null);
2506-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2506+
assertTrue(_doc.containsSource());
25072507
_doc.remove(0, _doc.getText().length());
25082508

25092509
_doc.insertString(0, "/*interface*/interface", null);
2510-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2510+
assertTrue(_doc.containsSource());
25112511
_doc.remove(0, _doc.getText().length());
25122512

25132513
_doc.insertString(0, "/*enum*/enum", null);
2514-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2514+
assertTrue(_doc.containsSource());
25152515
_doc.remove(0, _doc.getText().length());
25162516

25172517
_doc.insertString(0, "/*foo*/foo", null);
2518-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2518+
assertFalse(_doc.containsSource());
25192519
_doc.remove(0, _doc.getText().length());
25202520

25212521
_doc.insertString(0, "\"class\"class", null);
2522-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2522+
assertTrue(_doc.containsSource());
25232523
_doc.remove(0, _doc.getText().length());
25242524

25252525
_doc.insertString(0, "\"interface\"interface", null);
2526-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2526+
assertTrue(_doc.containsSource());
25272527
_doc.remove(0, _doc.getText().length());
25282528

25292529
_doc.insertString(0, "\"enum\"enum", null);
2530-
assertTrue(_doc.containsClassOrInterfaceOrEnum());
2530+
assertTrue(_doc.containsSource());
25312531
_doc.remove(0, _doc.getText().length());
25322532

25332533
_doc.insertString(0, "\"foo\"foo", null);
2534-
assertFalse(_doc.containsClassOrInterfaceOrEnum());
2534+
assertFalse(_doc.containsSource());
25352535
_doc.remove(0, _doc.getText().length());
25362536
}
25372537
}

drjava/src/edu/rice/cs/drjava/model/definitions/reducedmodel/ReducedModelState.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838

3939
import static edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelStates.*;
4040

41-
/** Represents the bstract notion of a shadowing state. The shadowing state of text is simply its interpretation during
41+
/** Represents the abstract notion of a shadowing state. The shadowing state of text is simply its interpretation during
4242
* compilation. Commented text is ignored; quoted text is accumulated into string constants. This classification
4343
* supports accurate highlighting, indenting, and other analyses of program text.
44-
* @version $Id$
45-
*/
44+
* @version $Id$
45+
*/
4646
public abstract class ReducedModelState {
4747

4848
abstract ReducedModelState update(TokenList.Iterator copyCursor);

0 commit comments

Comments
 (0)