Skip to content

Commit

Permalink
APSTUD-4778: fixed halt condition and also made improvement where ong…
Browse files Browse the repository at this point in the history
…oing parses will be seen and used instead of requesting new parses.
  • Loading branch information
fabioz committed May 15, 2012
1 parent e4649c4 commit a878a64
Show file tree
Hide file tree
Showing 25 changed files with 1,497 additions and 165 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -22,3 +22,6 @@ tests/com.aptana.scripting.tests/application-bundles/**/cache*.yml
tests/com.aptana.scripting.tests/user-bundles/**/cache*.yml
tests/com.aptana.scripting.tests/project-bundles/**/cache*.yml
tests/com.aptana.studio.tests.all/wintest
.metadata
.recordings
runtime-New_configuration/
108 changes: 108 additions & 0 deletions plugins/com.aptana.core/src/com/aptana/core/util/ImmutableTupleN.java
@@ -0,0 +1,108 @@
/**
* Aptana Studio
* Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
* Please see the license.html included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package com.aptana.core.util;

/**
* Defines a tuple of objects, adding equals and hashCode operations. It's expected that any object added to this tuple
* is immutable (properly implementing hashCode() and equals()).
*
* @author Fabio
*/
public class ImmutableTupleN
{

private final Object[] tuple;
private int hash;

public ImmutableTupleN(Object... tuple)
{
this.tuple = tuple;
}

@Override
public boolean equals(Object obj)
{
if (!(obj instanceof ImmutableTupleN))
{
return false;
}

ImmutableTupleN t2 = (ImmutableTupleN) obj;
if (t2.tuple.length != this.tuple.length)
{
return false;
}
for (int i = 0; i < tuple.length; i++)
{
Object o1 = tuple[i];
Object o2 = t2.tuple[i];
if (o1 != o2)
{
if (o1 == null || o2 == null)
{
return false;
}
if (!o1.equals(o2))
{
return false;
}
}
}

return true;
}

@Override
public int hashCode()
{
if (hash != 0)
{
return hash;
}
int ret = 1;
int len = tuple.length;
for (int i = 0; i < len; i++)
{
Object o = tuple[i];
if (o == null)
{
ret += (i * 3);
}
else
{
int objHash = o.hashCode();
ret += (i * 7 + (3 * objHash));
}
}
hash = 7 + ret;
return hash;
}

@Override
public String toString()
{
return this.getClass().getName() + "[" + StringUtil.join(", ", tuple) + "]";
}

/**
* @return the size of this tuple.
*/
public int size()
{
return this.tuple.length;
}

/**
* @param i
* the position of the element we want to get at the tuple.
*/
public Object getAt(int i)
{
return this.tuple[i];
}
}
28 changes: 28 additions & 0 deletions plugins/com.aptana.core/src/com/aptana/core/util/StringUtil.java
Expand Up @@ -344,6 +344,34 @@ public static String join(String delimiter, Collection<String> items)
return (items != null) ? join(delimiter, items.toArray(new String[items.size()])) : null;
}

/**
* Create a string by concatenating the elements of a collection using a delimiter between each item
*
* @param delimiter
* The text to place between each element in the array
* @param items
* The array of items
* @return The resulting string
*/
public static String join(String delimiter, Object... items)
{
String[] s = new String[items.length];
for (int i = 0; i < items.length; i++)
{
Object item = items[i];
if (item == null)
{
s[i] = "null"; //$NON-NLS-1$

}
else
{
s[i] = item.toString();
}
}
return join(delimiter, s);
}

/**
* Create a string by concatenating the elements of a string array using a delimiter between each item
*
Expand Down
Expand Up @@ -11,8 +11,9 @@

import com.aptana.core.logging.IdeLog;
import com.aptana.editor.js.JSPlugin;
import com.aptana.parsing.IParseState;
import com.aptana.parsing.IParseStateCacheKey;
import com.aptana.parsing.ParseState;
import com.aptana.parsing.ParseStateCacheKeyWithComments;

/**
* JSParseState
Expand Down Expand Up @@ -122,32 +123,11 @@ public void setCollectComments(boolean flag)
commentContentStack.peek().attachComments = flag;
}

public boolean requiresReparse(IParseState newState)
@Override
public IParseStateCacheKey getCacheKey(String contentTypeId)
{
// We can't compare, assume re-parse
if (!(newState instanceof JSParseState))
{
return true;
}

if (super.requiresReparse(newState))
{
return true;
}

JSParseState newParseState = (JSParseState) newState;
if (newParseState.attachComments() && !attachComments())
{
// we need comments attached, and old one doesn't have them attached. re-parse
return true;
}

if (newParseState.collectComments() && !collectComments())
{
// we need comments collected, and old one doesn't have them collected. re-parse
return true;
}

return false;
IParseStateCacheKey cacheKey = super.getCacheKey(contentTypeId);
return new ParseStateCacheKeyWithComments(attachComments(), collectComments(), cacheKey);
}

}
@@ -1,3 +1,10 @@
/**
* Aptana Studio
* Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
* Please see the license.html included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package com.aptana.index.core.build;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -28,6 +35,7 @@
import com.aptana.core.util.StringUtil;
import com.aptana.index.core.IndexPlugin;
import com.aptana.parsing.IParseState;
import com.aptana.parsing.IParseStateCacheKey;
import com.aptana.parsing.ParseState;
import com.aptana.parsing.ParserPoolFactory;
import com.aptana.parsing.ast.IParseError;
Expand All @@ -41,6 +49,7 @@ public class BuildContext
private IFile file;
protected Map<String, Collection<IProblem>> problems;
private IParseState fParseState;
private IParseStateCacheKey fParseStateCacheKey;

private String fContents;

Expand Down Expand Up @@ -90,43 +99,32 @@ public synchronized IParseRootNode getAST(IParseState parseState) throws CoreExc
parseState.setEditState(getContents());
try
{
boolean reparse = false;
if (fParseState == null)
String contentType = getContentType();
IParseStateCacheKey newCacheKey = parseState.getCacheKey(contentType);
if (fParseState != null && fParseStateCacheKey != null
&& !fParseStateCacheKey.requiresReparse(newCacheKey))
{
reparse = true;
// copy over errors from old parse state to new one since we're not re-parsing
parseState.copyErrorsFrom(fParseState);
return fParseState.getParseResult();
}
else
fParseState = parseState;
fParseStateCacheKey = newCacheKey;
// FIXME What if we fail to parse? Should we catch and log that exception here and return null?
try
{
reparse = fParseState.requiresReparse(parseState);
if (!reparse)
{
// copy over errors from old parse state to new one since we're not re-parsing
for (IParseError error : fParseState.getErrors())
{
parseState.addError(error);
}
}
// FIXME The parsers need to throw a specific SyntaxException or something for us to differentiate
// between those and IO errors!
IParseRootNode ast = parse(contentType, fParseState);
fParseState.setParseResult(ast);
}

if (reparse)
catch (CoreException e)
{
fParseState = parseState;
// FIXME What if we fail to parse? Should we catch and log that exception here and return null?
try
{
// FIXME The parsers need to throw a specific SyntaxException or something for us to differentiate
// between those and IO errors!
IParseRootNode ast = ParserPoolFactory.parse(getContentType(), fParseState);
fParseState.setParseResult(ast);
}
catch (CoreException e)
{
throw e;
}
catch (Exception e)
{
throw new CoreException(new Status(IStatus.ERROR, IndexPlugin.PLUGIN_ID, e.getMessage(), e));
}
throw e;
}
catch (Exception e)
{
throw new CoreException(new Status(IStatus.ERROR, IndexPlugin.PLUGIN_ID, e.getMessage(), e));
}
if (fParseState == null)
{
Expand All @@ -144,9 +142,15 @@ public synchronized IParseRootNode getAST(IParseState parseState) throws CoreExc
}
}

protected IParseRootNode parse(String contentType, IParseState parseState) throws Exception
{
return ParserPoolFactory.parse(contentType, parseState);
}

public synchronized void resetAST()
{
fParseState = null;
fParseStateCacheKey = null;
}

public synchronized String getContents()
Expand Down
Expand Up @@ -127,12 +127,15 @@ public interface IParseState
public void setProgressMonitor(IProgressMonitor monitor);

/**
* Given the new parse state, does this old one encompass the requirements? (i.e. can we just re-use the parse
* result from this parse state instead of doing a re-parse?)
* @return an immutable object to be used as the key for this parse state (i.e.: object with hashCode/equals).
*/
public IParseStateCacheKey getCacheKey(String contentTypeId);

/**
* Copies the errors from the cachedParseState to this parse state.
*
* @param newState
* @return
* @param cachedParseState errors will be copied from this parse state.
*/
public boolean requiresReparse(IParseState newState);
public void copyErrorsFrom(IParseState cachedParseState);

}
@@ -0,0 +1,33 @@
/**
* Aptana Studio
* Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
* Please see the license.html included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package com.aptana.parsing;

/**
* @author Fabio
*/
public interface IParseStateCacheKey
{

/**
* @return if a reparse is required. It may be possible that 2 keys are equal but a reparse is still needed. (i.e.:
* done given the need of having a result with or without comments -- in which case having the comments is
* accepted if we ask for it without the comments but the other way around would need a reparse).
*/
public boolean requiresReparse(IParseStateCacheKey newCacheKey);

/**
* Just making explicit that we must have equals/hashCode properly implemented as this will be a key in a map.
*/
public boolean equals(Object other);

/**
* Just making explicit that we must have equals/hashCode properly implemented as this will be a key in a map.
*/
public int hashCode();

}
Expand Up @@ -16,6 +16,7 @@
public class Messages extends NLS
{
private static final String BUNDLE_NAME = "com.aptana.parsing.messages"; //$NON-NLS-1$
public static String ParserPoolFactory_Expecting_Source;
public static String ParserPoolFactory_Cannot_Acquire_Parser;
public static String ParserPoolFactory_Cannot_Acquire_Parser_Pool;
static
Expand Down

0 comments on commit a878a64

Please sign in to comment.