* Created on 05.03.2003
*
* @author Stefan Langer (musk)
- * @version $Revision: 1.10 $
+ * @version $Revision: 1.11 $
*/
package net.sourceforge.phpeclipse.phpeditor.php;
*/
public class PHPPartitionScanner implements IPartitionTokenScanner
{
-// private final int HTML = 0;
-// private final int PHP = 1;
-// private final int JS = 2;
-// private final int CSS = 4;
-// private final int COMMENT = 5;
-// private final int HTMLCOMMENT = 6;
-
+ private static final boolean DEBUG = false;
private IDocument fDocument = null;
private int fOffset = -1;
-// private int fLastOffset = -1;
private String fContentType = IPHPPartitionScannerConstants.HTML;
-
- private boolean partitionBorder = false;
+ private String fPrevContentType;
+
+ private boolean partitionBorder = false;
private int fTokenOffset;
private int fEnd = -1;
private int fLength;
- //private int fState = HTML;
private Map tokens = new HashMap();
public PHPPartitionScanner()
this.tokens.put(
IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
new Token(IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT));
+ this.tokens.put(
+ IDocument.DEFAULT_CONTENT_TYPE,
+ new Token(IDocument.DEFAULT_CONTENT_TYPE));
}
private IToken getToken(String type)
{
- fLength = fOffset-fTokenOffset;
- IToken token = (IToken)this.tokens.get(type);
- Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
- return token;
+ fLength = fOffset - fTokenOffset;
+ IToken token = (IToken) this.tokens.get(type);
+ Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
+ if (DEBUG)
+ {
+ System.out.println(
+ "Partition: fTokenOffset="
+ + fTokenOffset
+ + " fContentType="
+ + fContentType
+ + " fLength="
+ + fLength);
+ }
+ return token;
}
-/* (non-Javadoc)
- * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
- */
-public void setPartialRange(
- IDocument document,
- int offset,
- int length,
- String contentType,
- int partitionOffset)
-{
- this.setRange(document, offset, length);
- if(this.tokens.containsKey(contentType))
- fContentType = contentType;
- if (partitionOffset > -1)
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
+ */
+ public void setPartialRange(
+ IDocument document,
+ int offset,
+ int length,
+ String contentType,
+ int partitionOffset)
{
- partitionBorder = false;
- fTokenOffset = partitionOffset;
+ this.setRange(document, offset, length);
+ if (DEBUG)
+ {
+ System.out.println(
+ "PartialRange: contentType="
+ + contentType
+ + " partitionOffset="
+ + partitionOffset);
+ }
+
+ if (this.tokens.containsKey(contentType))
+ fContentType = contentType;
+ // TODO Calculate previouse contenttype
+ if (partitionOffset > -1)
+ {
+ partitionBorder = false;
+ fTokenOffset = partitionOffset;
+ }
}
-}
/* (non-Javadoc)
* @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
* @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
*/
public int getTokenOffset()
- {
- return fTokenOffset;
+ {
+ return fTokenOffset;
}
/* (non-Javadoc)
switch (c)
{
case '<' :
- if (fContentType != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true))
+ if (fContentType
+ != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
+ && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true))
{
-
- if (fContentType != IPHPPartitionScannerConstants.PHP
+ if (fContentType != IPHPPartitionScannerConstants.PHP
&& fOffset - 5 > 0)
{
fOffset -= 5;
IToken token = getToken(fContentType);
+ // save previouse contenttype
+ fPrevContentType = fContentType;
+
fContentType = IPHPPartitionScannerConstants.PHP;
return token;
{
fOffset -= 4;
IToken token = getToken(fContentType);
+
fContentType =
IPHPPartitionScannerConstants
.HTML_MULTILINE_COMMENT;
if (fContentType == IPHPPartitionScannerConstants.PHP)
{
if ((c = read()) == '>')
- {
- fContentType = IPHPPartitionScannerConstants.HTML;
+ { // TODO Actually calculate the previouse contenttype from the document
+ if(fPrevContentType != null)
+ fContentType = fPrevContentType;
+ else
+ fContentType = IPHPPartitionScannerConstants.HTML;
partitionBorder = true;
return getToken(IPHPPartitionScannerConstants.PHP);
}
- else if(c != ICharacterScanner.EOF)
+ else if (c != ICharacterScanner.EOF)
unread();
}
break;
}
break;
case '/' :
- if ((c=read()) == '*')
+ if ((c = read()) == '*')
{ // MULTINE COMMENT JAVASCRIPT, CSS, PHP
if (fContentType == IPHPPartitionScannerConstants.PHP
&& fOffset - 2 > 0)
{
fOffset -= 2;
IToken token = getToken(fContentType);
+
fContentType =
IPHPPartitionScannerConstants
.PHP_MULTILINE_COMMENT;
}
}
- else if(c != ICharacterScanner.EOF)
+ else if (c != ICharacterScanner.EOF)
unread();
break;
case '*' :
{
fContentType = IPHPPartitionScannerConstants.PHP;
partitionBorder = true;
+
return getToken(
IPHPPartitionScannerConstants
.PHP_MULTILINE_COMMENT);
{
}
}
- else if(c != ICharacterScanner.EOF)
+ else if (c != ICharacterScanner.EOF)
unread();
break;
}
*/
public void setRange(IDocument document, int offset, int length)
{
+ if (DEBUG)
+ {
+ System.out.println(
+ "SET RANGE: offset=" + offset + " length=" + length);
+ }
+
fDocument = document;
fOffset = offset;
fTokenOffset = offset;
- fLength = 0;
+ fLength = 0;
fEnd = fOffset + length;
- //partitionBorder = false;
+ //partitionBorder = false;
}
private int read()
{
try
{
- if (fOffset < fEnd)
+ if (fOffset < fEnd)
{
- return fDocument.getChar(fOffset++);
+ return fDocument.getChar(fOffset++);
}
- return ICharacterScanner.EOF;
+ return ICharacterScanner.EOF;
}
catch (BadLocationException e)
{
*/
private boolean checkPattern(char[] pattern, boolean ignoreCase)
{
- int prevOffset = fOffset;
+ int prevOffset = fOffset;
for (int i = 0; i < pattern.length; i++)
{
int c = read();
-
- if(c == ICharacterScanner.EOF || !letterEquals(c, pattern[i], ignoreCase))
+
+ if (c == ICharacterScanner.EOF
+ || !letterEquals(c, pattern[i], ignoreCase))
{
- fOffset = prevOffset;
+ fOffset = prevOffset;
return false;
}
}
return true;
}
-
+
private boolean letterEquals(int test, char letter, boolean ignoreCase)
{
- if(test == letter)
- return true;
- else if(ignoreCase && Character.isLowerCase(letter) && test == Character.toUpperCase(letter))
- return true;
- else if(ignoreCase && Character.isUpperCase(letter) && test == Character.toLowerCase(letter))
- return true;
-
- return false;
+ if (test == letter)
+ return true;
+ else if (
+ ignoreCase
+ && Character.isLowerCase(letter)
+ && test == Character.toUpperCase(letter))
+ return true;
+ else if (
+ ignoreCase
+ && Character.isUpperCase(letter)
+ && test == Character.toLowerCase(letter))
+ return true;
+
+ return false;
}
-
+
}