Skip to content

Commit

Permalink
Fix for issue #288: add some guard conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 8, 2017
1 parent 55ae0f8 commit f165358
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,17 @@ private String checkQualifiedType(TypeLookupResult result, IJavaElement enclosin
int typeStart = startOffset(type), typeEnd = endOffset(type);
type = GroovyUtils.getBaseType(type); // unpack type now that position is known

if (typeEnd > 0) {
String source = gunit.getSource().substring(typeStart, typeEnd);
if (typeStart >= 0 && typeEnd > typeStart) {
String gunitSource = gunit.getSource();
if (typeEnd == gunitSource.length() + 1)
typeEnd = gunitSource.length(); // off by one?
else if (typeEnd > gunitSource.length()) return null;
String source = gunitSource.substring(typeStart, typeEnd);
int nameStart = typeStart + source.indexOf(GroovyUtils.splitName(type)[1]);

// check for code selection on the type name's qualifier string
if (nameStart > typeStart && nameStart > selectRegion.getEnd() && selectRegion.getEnd() > typeStart) {
String selected = gunit.getSource().substring(typeStart, selectRegion.getEnd());
String selected = gunitSource.substring(typeStart, selectRegion.getEnd());
selected = selected.replaceAll("\\.$", ""); // remove any trailing dot
String qualifier = GroovyUtils.splitName(type)[0].replace('$', '.');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 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 @@ -28,8 +28,6 @@
* <li>thing[10].value</li>
* <li>[1, 2, 3].collect { it.toString() }. // Note the '.'</li>
* </ul>
*
* @author empovazan
*/
public class ExpressionFinder {
/**
Expand Down Expand Up @@ -112,8 +110,7 @@ public String findForCompletions(ISourceBuffer sourceBuffer, int offset) throws
*
* @param buffer the document to search
* @param initialOffset the initial offset
* @return the offset of the first non-word character starting at
* initialOffset
* @return the offset of the first non-word character starting at initialOffset
*/
public int findTokenEnd(ISourceBuffer buffer, int initialOffset) {
int candidate = initialOffset;
Expand All @@ -130,9 +127,7 @@ public int findTokenEnd(ISourceBuffer buffer, int initialOffset) {
* Splits the given expression into two parts: the type evaluation part, and
* the code completion part.
*
* @param expression
* The expression returned by the
* {@link #findForCompletions(ISourceBuffer, int)} method.
* @param expression returned by the {@link #findForCompletions(ISourceBuffer, int)} method
* @return A string pair, the expression to complete, and the prefix to be
* completed.<br>
* { "", null } if no completion expression could be found
Expand Down Expand Up @@ -201,7 +196,7 @@ public String[] splitForCompletionNoTrim(String expression) {
return ret;
}

public class NameAndLocation {
public static class NameAndLocation {
public final String name;

public final int location;
Expand Down Expand Up @@ -233,13 +228,11 @@ public int dims() {
}

public NameAndLocation findPreviousTypeNameToken(ISourceBuffer buffer, int start) {
int current = start;
current--;
while (current >= 0 && !Character.isWhitespace(buffer.charAt(current))
&& Character.isJavaIdentifierPart(buffer.charAt(current))) {
current--;
int current = Math.min(start, buffer.length()) - 1;
while (current >= 0 && !Character.isWhitespace(buffer.charAt(current)) &&
Character.isJavaIdentifierPart(buffer.charAt(current))) {
current -= 1;
}

if (current < 0 || !Character.isWhitespace(buffer.charAt(current))) {
return null;
}
Expand Down

0 comments on commit f165358

Please sign in to comment.