Skip to content

Commit

Permalink
Merge pull request #211 from eric-milles/small-fixes
Browse files Browse the repository at this point in the history
Fixes for several minor issues
  • Loading branch information
aclement authored Oct 31, 2016
2 parents 4c2b291 + 6efda74 commit 65f1525
Show file tree
Hide file tree
Showing 25 changed files with 1,620 additions and 1,673 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 the original author or authors.
* Copyright 2009-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,51 +31,46 @@
import org.eclipse.jdt.ui.CodeStyleConfiguration;

/**
*
* @author Andrew Eisenberg
* @author Nieraj Singh
* @created Oct 27, 2009
*/
public class GroovyImportRewriteFactory {

private static final Pattern IMPORTS_PATTERN = Pattern
.compile("(\\A|[\\n\\r])import\\s");

private static final Pattern PACKAGE_PATTERN = Pattern
.compile("(\\A|[\\n\\r])package\\s");

private static final Pattern EOL_PATTERN = Pattern.compile("($|[\\n\\r])");

private ImportRewrite rewrite;
// set to true if there is a problem creating the rewrite
private boolean cantCreateRewrite = false;

/**
* This should never be null
*/
private GroovyCompilationUnit unit;

/**
* This may be null
*/
private ModuleNode module;

/**
* If this constructor is used, the a check may be performed on the module
* for unrecoverable errors before generating an import rewrite. Compilation
* unit should never be null, although the module can be null
*/
public GroovyImportRewriteFactory(GroovyCompilationUnit unit, ModuleNode module) {
this.unit = unit;
this.module = module;
}

/**
* Module is null in this case. Only a compilation unit is passed.
*/
public GroovyImportRewriteFactory(GroovyCompilationUnit unit) {
this.unit = unit;
}
private static final Pattern IMPORTS_PATTERN = Pattern.compile("(\\A|[\\n\\r])import\\s");
private static final Pattern PACKAGE_PATTERN = Pattern.compile("(\\A|[\\n\\r])package\\s");
private static final Pattern EOL_PATTERN = Pattern.compile("($|[\\n\\r])");

private ImportRewrite rewrite;
// set to true if there is a problem creating the rewrite
private boolean cantCreateRewrite = false;

/**
* This should never be null
*/
private GroovyCompilationUnit unit;

/**
* This may be null
*/
private ModuleNode module;

/**
* If this constructor is used, the a check may be performed on the module
* for unrecoverable errors before generating an import rewrite. Compilation
* unit should never be null, although the module can be null
*/
public GroovyImportRewriteFactory(GroovyCompilationUnit unit, ModuleNode module) {
this.unit = unit;
this.module = module;
}

/**
* Module is null in this case. Only a compilation unit is passed.
*/
public GroovyImportRewriteFactory(GroovyCompilationUnit unit) {
this.unit = unit;
}

public static int astlevel = -1;

Expand All @@ -92,111 +87,104 @@ public static int getAstLevel() {
return astlevel;
}

/**
* Returns an import rewrite for the module node only if
* ModuleNode.encounteredUnrecoverableError()
*
* Tries to find the start and end locations of the import statements. Makes
* a best guess using regular expression. This method ensures that even if
* the ComplationUnit is unparseable, the imports are still placed in the
* correct location.
*
* @return an {@link ImportRewrite} for the ModuleNode if it encountered an
* unrecoverable error, or null if no problems.
*/
public ImportRewrite getImportRewrite(IProgressMonitor monitor) {
/**
* Returns an import rewrite for the module node only if
* ModuleNode.encounteredUnrecoverableError()
*
* Tries to find the start and end locations of the import statements. Makes
* a best guess using regular expression. This method ensures that even if
* the ComplationUnit is unparseable, the imports are still placed in the
* correct location.
*
* @return an {@link ImportRewrite} for the ModuleNode if it encountered an
* unrecoverable error, or null if no problems.
*/
public ImportRewrite getImportRewrite(IProgressMonitor monitor) {

// For the case of organize imports, if no unrecoverable error has been
// found,
// then we don't do any work here instead, a standard import rewrite is
// used.
// For the case of add import, this special rewrite will always be used
// (and the module field will be null)
if (module != null && !module.encounteredUnrecoverableError()) {
return null;
}
if (module != null && !module.encounteredUnrecoverableError()) {
return null;
}

if (rewrite == null && !cantCreateRewrite) {
if (rewrite == null && !cantCreateRewrite) {

// find a reasonable substring that contains
// what looks to be the import dependencies
CharArraySequence contents = new CharArraySequence(
unit.getContents());
CharArraySequence imports = findImportsRegion(contents);
// find a reasonable substring that contains
// what looks to be the import dependencies
CharArraySequence contents = new CharArraySequence(
unit.getContents());
CharArraySequence imports = findImportsRegion(contents);

// Now send this to a parser
// need to be very careful here that if we can't parse, then
// don't send to rewriter
// Now send this to a parser
// need to be very careful here that if we can't parse, then don't send to rewriter

ASTParser parser = ASTParser.newParser(getAstLevel());
parser.setSource(unit.cloneCachingContents(CharOperation.concat(
imports.getChars(), "\nclass X { }".toCharArray())));
parser.setKind(ASTParser.K_COMPILATION_UNIT);
ASTNode result = null;
try {
result = parser.createAST(monitor);
} catch (IllegalStateException e) {
GroovyCore.logException("Can't create ImportRewrite for:\n"
+ imports, e);
}
if (result instanceof CompilationUnit) {
rewrite = CodeStyleConfiguration.createImportRewrite(
(CompilationUnit) result, true);

} else {
// something wierd happened.
// ensure we don't try again
cantCreateRewrite = true;
}
}

return rewrite;
}

/**
* Convenience methpd for
* {@link GroovyProposalTypeSearchRequestor#findImportsRegion(CharArraySequence)}
*/
public static CharArraySequence findImportsRegion(String contents) {
return findImportsRegion(new CharArraySequence(contents));
}

/**
* Finds a region of text that kind of looks like where the imports should
* be placed. Uses regular expressions.
*
* @param contents
* the contents of a compilation unit
* @return a presumed region
*/
public static CharArraySequence findImportsRegion(CharArraySequence contents) {
// heuristics:
// look for last index of ^import
// if that returns -1, then look for ^package
Matcher matcher = IMPORTS_PATTERN.matcher(contents);
int importsEnd = 0;
while (matcher.find(importsEnd)) {
importsEnd = matcher.end();
}

if (importsEnd == 0) {
// no imports found, look for package declaration
matcher = PACKAGE_PATTERN.matcher(contents);
if (matcher.find()) {
importsEnd = matcher.end();
}

}

if (importsEnd > 0) {
// look for end of line
matcher = EOL_PATTERN.matcher(contents);
if (matcher.find(importsEnd)) {
importsEnd = matcher.end();
}
}

return contents.subSequence(0, importsEnd);
}
parser.setSource(unit.cloneCachingContents(CharOperation.concat(imports.getChars(), "class X { }".toCharArray())));
parser.setKind(ASTParser.K_COMPILATION_UNIT);
ASTNode result = null;
try {
result = parser.createAST(monitor);
} catch (IllegalStateException e) {
GroovyCore.logException("Can't create ImportRewrite for:\n" + imports, e);
}
if (result instanceof CompilationUnit) {
rewrite = CodeStyleConfiguration.createImportRewrite((CompilationUnit) result, true);
} else {
// something wierd happened.
// ensure we don't try again
cantCreateRewrite = true;
}
}

return rewrite;
}

/**
* Convenience methpd for
* {@link GroovyProposalTypeSearchRequestor#findImportsRegion(CharArraySequence)}
*/
public static CharArraySequence findImportsRegion(String contents) {
return findImportsRegion(new CharArraySequence(contents));
}

/**
* Finds a region of text that kind of looks like where the imports should
* be placed. Uses regular expressions.
*
* @param contents
* the contents of a compilation unit
* @return a presumed region
*/
public static CharArraySequence findImportsRegion(CharArraySequence contents) {
// heuristics:
// look for last index of ^import
// if that returns -1, then look for ^package
Matcher matcher = IMPORTS_PATTERN.matcher(contents);
int importsEnd = 0;
while (matcher.find(importsEnd)) {
importsEnd = matcher.end();
}

if (importsEnd == 0) {
// no imports found, look for package declaration
matcher = PACKAGE_PATTERN.matcher(contents);
if (matcher.find()) {
importsEnd = matcher.end();
}
}

if (importsEnd > 0) {
// look for end of line
matcher = EOL_PATTERN.matcher(contents);
if (matcher.find(importsEnd)) {
importsEnd = matcher.end();
}
}

return contents.subSequence(0, importsEnd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
/**
* @author Andrew Eisenberg
* @created Nov 10, 2009
*
*/
public class TypeCompletionProcessor extends AbstractGroovyCompletionProcessor {

Expand Down Expand Up @@ -70,16 +69,13 @@ context, getJavaContext(), expressionStart,
context.completionEnd - expressionStart,
getNameEnvironment().nameLookup, monitor);

getNameEnvironment().findTypes(toSearch.toCharArray(), true, // all member
// types, should
// be false when
// in
// constructor
getNameEnvironment().findTypes(toSearch.toCharArray(),
true, // all member types, should be false when in constructor
true, // camel case match
getSearchFor(), requestor, monitor);

List<ICompletionProposal> typeProposals = requestor
.processAcceptedTypes();
List<ICompletionProposal> typeProposals = requestor.processAcceptedTypes();

return typeProposals;
}

Expand All @@ -88,10 +84,6 @@ context, getJavaContext(), expressionStart,
* Don't show types if there is a '.'
* Don't show types when in a class body and there is a type declaration
* immediately before
*
* @param context
* @param toSearch
* @return
*/
private boolean shouldShowTypes(ContentAssistContext context,
String toSearch) {
Expand All @@ -100,10 +92,6 @@ private boolean shouldShowTypes(ContentAssistContext context,
|| isBeforeTypeName(context.location, context.unit, context.completionLocation);
}

/**
* @param context
* @return
*/
private int findExpressionStart(ContentAssistContext context) {
// remove "new"
int completionLength;
Expand All @@ -117,9 +105,6 @@ private int findExpressionStart(ContentAssistContext context) {
return expressionStart;
}

/**
* @return
*/
private int getSearchFor() {
switch(getContext().location) {
case EXTENDS:
Expand Down Expand Up @@ -151,5 +136,4 @@ private boolean isBeforeTypeName(ContentAssistLocation location, GroovyCompilati
return !FIELD_MODIFIERS.contains(nameAndLocation.name.trim());
// GRECLIPSE end
}

}
Loading

0 comments on commit 65f1525

Please sign in to comment.