PageRenderTime 163ms CodeModel.GetById 113ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/css/CssSideKickParser.java

#
Java | 127 lines | 58 code | 27 blank | 42 comment | 4 complexity | 4fc9aea3cf6bb1d1e32d675c772b8b75 MD5 | raw file
  1/**
  2 * CssSideKickParser.java
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 2006 Jakub Roztocil
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21
 22
 23package sidekick.css;
 24
 25//{{{ Imports
 26import errorlist.*;
 27import java.util.regex.*;
 28import javax.swing.tree.*;
 29import org.gjt.sp.jedit.*;
 30import org.gjt.sp.util.Log;
 31import sidekick.*;
 32import sidekick.SideKickParsedData;
 33import sidekick.SideKickParser;
 34import sidekick.enhanced.*; 
 35//}}}
 36
 37/**
 38 * @deprecated use CSS2SideKickParser instead.
 39 */
 40public class CssSideKickParser extends SideKickParser {
 41
 42	Pattern selRe = Pattern.compile("((^|[}{])\\s*)+(.+?)(\\s*\\{)",  Pattern.DOTALL);
 43	Pattern commentRe = Pattern.compile("\\s*\\/\\*.*?\\*\\/\\s*|\n",  Pattern.DOTALL);
 44	
 45	String text;
 46
 47	//{{{ CssSideKickParser constructor
 48	public CssSideKickParser() {
 49		super("css");
 50	} //}}}
 51
 52	//{{{ parse() method
 53	public SideKickParsedData parse(Buffer buffer, DefaultErrorSource errorSource) {
 54		int startPos;
 55		int endPos;
 56		
 57		Matcher selMatcher;		// matcher for selectors
 58		Matcher commentMatcher;	// matcher for coments
 59		String[] tlc;			// top level commads - selectors, @rules
 60
 61		SideKickParsedData data = new SideKickParsedData(buffer.getName());
 62		text = buffer.getText(0, buffer.getLength() -1);
 63		
 64		selMatcher = selRe.matcher(text);
 65
 66		// selMatcher should find only selectors, but if there are some
 67		// at-rules like @import or @media, they are matched also.
 68		// @imports are almost OK, but for all at-rules is startPos incorrect
 69		// and first selector inside @media rule is not shown
 70		
 71		// TODO: correct parsing of at-rulles
 72		while (selMatcher.find()) {
 73			
 74			// Start position
 75			startPos = selMatcher.start() + selMatcher.group(1).length() + selMatcher.group(3).length();
 76			
 77			// Remove comments and redundant white spaces  from selector list: 
 78			// div#menu  \n   /* this is cool selector */ ul    li ===> div#menu ul li
 79			commentMatcher = commentRe.matcher(selMatcher.group(3));
 80			tlc = commentMatcher.replaceAll(" ").trim().split("\\s*;\\s*");
 81			// Log.log(Log.DEBUG, CssSideKickParser.class, tlc);
 82			
 83			for (int i = 0; i < tlc.length; i++) {
 84				endPos = selMatcher.end() + buffer.getText(selMatcher.end(), buffer.getLength() - 1 - selMatcher.end()).indexOf("}");
 85				
 86				if (endPos == -1) {
 87					endPos = buffer.getLength() -1;
 88				} 
 89				
 90				// Log.log(Log.DEBUG, CssSideKickParser.class, "Start: " + startPos + " End: " + endPos);
 91				 
 92				SourceAsset asset = new SourceAsset(tlc[i], getLineNo(startPos), 	buffer.createPosition(startPos));
 93				IAsset iasset = (IAsset) asset;
 94				
 95				// Log.log(Log.DEBUG, CssSideKickParser.class, iasset);
 96				
 97				iasset.setStart(buffer.createPosition(startPos));
 98				iasset.setEnd(buffer.createPosition(endPos)); 
 99				data.root.add(new DefaultMutableTreeNode(iasset));
100			}
101		}
102
103		return data;
104	} //}}}
105
106	//{{{ getLineNo() method
107	public int getLineNo(int start) {
108		return text.substring(0, start).split("\n").length;
109	} //}}}
110
111	//{{{ supportsCompletion() method
112	public boolean supportsCompletion() {
113		return true;
114	} //}}}
115	
116	//{{{ canCompleteAnywhere() method
117	public boolean canCompleteAnywhere() {
118		return true;
119	} //}}}
120	
121	//{{{ complete() method
122	public SideKickCompletion complete(EditPane editPane, int caret) {
123		CompletionRequest cr = new CompletionRequest(editPane, caret);
124		return cr.getSideKickCompletion();
125	} //}}}
126
127}