update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / search / JspIndexPatternBuilder.java
blob363c3cec3ccde5fb0418f698fd7ebac07210a789
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.impl.search;
18 import com.intellij.lang.LanguageParserDefinitions;
19 import com.intellij.lang.ParserDefinition;
20 import com.intellij.lexer.Lexer;
21 import com.intellij.lexer.LexerBase;
22 import com.intellij.openapi.editor.Document;
23 import com.intellij.openapi.editor.highlighter.EditorHighlighter;
24 import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
25 import com.intellij.openapi.editor.highlighter.HighlighterIterator;
26 import com.intellij.openapi.editor.impl.DocumentImpl;
27 import com.intellij.psi.PsiDocumentManager;
28 import com.intellij.psi.PsiFile;
29 import com.intellij.psi.JspPsiUtil;
30 import com.intellij.psi.impl.cache.impl.id.IdTableBuilding;
31 import com.intellij.psi.impl.source.tree.StdTokenSets;
32 import com.intellij.psi.jsp.JspFile;
33 import com.intellij.psi.jsp.JspTokenType;
34 import com.intellij.psi.tree.IElementType;
35 import com.intellij.psi.tree.TokenSet;
36 import com.intellij.util.text.CharSequenceSubSequence;
38 /**
39 * @author yole
41 public class JspIndexPatternBuilder implements IndexPatternBuilder {
42 public Lexer getIndexingLexer(final PsiFile file) {
43 if (JspPsiUtil.isInJspFile(file)) {
44 EditorHighlighter highlighter;
46 final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
47 EditorHighlighter cachedEditorHighlighter;
48 boolean alreadyInitializedHighlighter = false;
50 if (document instanceof DocumentImpl &&
51 (cachedEditorHighlighter = ((DocumentImpl)document).getEditorHighlighterForCachesBuilding()) != null &&
52 IdTableBuilding.checkCanUseCachedEditorHighlighter(file.getText(), cachedEditorHighlighter)
53 ) {
54 highlighter = cachedEditorHighlighter;
55 alreadyInitializedHighlighter = true;
56 } else {
57 highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(file.getProject(),file.getVirtualFile());
59 return new LexerEditorHighlighterLexer(highlighter, alreadyInitializedHighlighter);
61 return null;
64 public TokenSet getCommentTokenSet(final PsiFile file) {
65 final JspFile jspFile = JspPsiUtil.getJspFile(file);
66 TokenSet commentTokens = TokenSet.orSet(JavaIndexPatternBuilder.XML_COMMENT_BIT_SET, StdTokenSets.COMMENT_BIT_SET);
67 final ParserDefinition parserDefinition =
68 LanguageParserDefinitions.INSTANCE.forLanguage(jspFile.getViewProvider().getTemplateDataLanguage());
69 if (parserDefinition != null) {
70 commentTokens = TokenSet.orSet(commentTokens, parserDefinition.getCommentTokens());
72 return commentTokens;
75 public int getCommentStartDelta(final IElementType tokenType) {
76 return tokenType == JspTokenType.JSP_COMMENT ? "<%--".length() : 0;
79 public int getCommentEndDelta(final IElementType tokenType) {
80 return tokenType == JspTokenType.JSP_COMMENT ? "--%>".length() : 0;
83 private static class LexerEditorHighlighterLexer extends LexerBase {
84 HighlighterIterator iterator;
85 CharSequence buffer;
86 int start;
87 int end;
88 private final EditorHighlighter myHighlighter;
89 private final boolean myAlreadyInitializedHighlighter;
91 public LexerEditorHighlighterLexer(final EditorHighlighter highlighter, boolean alreadyInitializedHighlighter) {
92 myHighlighter = highlighter;
93 myAlreadyInitializedHighlighter = alreadyInitializedHighlighter;
96 public void start(CharSequence buffer, int startOffset, int endOffset, int state) {
97 if (myAlreadyInitializedHighlighter) {
98 this.buffer = buffer;
99 start = startOffset;
100 end = endOffset;
101 } else {
102 myHighlighter.setText(new CharSequenceSubSequence(this.buffer = buffer, start = startOffset, end = endOffset));
104 iterator = myHighlighter.createIterator(0);
107 public int getState() {
108 return 0;
111 public IElementType getTokenType() {
112 if (iterator.atEnd()) return null;
113 return iterator.getTokenType();
116 public int getTokenStart() {
117 return iterator.getStart();
120 public int getTokenEnd() {
121 return iterator.getEnd();
124 public void advance() {
125 iterator.advance();
128 public CharSequence getBufferSequence() {
129 return buffer;
132 public int getBufferEnd() {
133 return end;