|
From: Christopher W. <caw...@us...> - 2005-09-18 15:10:13
|
Update of /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28253/src/org/rubypeople/rdt/internal/ui Modified Files: RubyFileMatcher.java Log Message: recognize "Rakefile" as a ruby file. recognize ".yml" files as ruby related. read first line of a file (if it is unknwon if it's ruby related) and check for '#!' and 'ruby'. If they're there, assume the file is a ruby file. (NOTE: This may slow down opening/creating a ruby project when the project has a large set of 'unknown' files [don't match normal names or extensions for ruby]) Index: RubyFileMatcher.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/RubyFileMatcher.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RubyFileMatcher.java 12 Mar 2005 15:32:31 -0000 1.2 --- RubyFileMatcher.java 18 Sep 2005 15:10:05 -0000 1.3 *************** *** 12,15 **** --- 12,18 ---- package org.rubypeople.rdt.internal.ui; + import java.io.BufferedReader; + import java.io.IOException; + import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collection; *************** *** 19,22 **** --- 22,26 ---- import org.eclipse.core.resources.IFile; + import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.util.ListenerList; *************** *** 31,73 **** /** * RubyViewerFilter uses the Editor mappings for recognising and filtering files ! * Both Editor mappings from plugin.xml and creating using the preferences page are considered */ public class RubyFileMatcher { public static final int PROP_MATCH_CRITERIA = 1; ! private String[] rubyFileExtensions; ! private String[] rubyFileNames; ! private ListenerList propChangeListeners; private IPropertyListener propertyListener = new IPropertyListener() { ! public void propertyChanged(Object source, int property) { ! if (property == IEditorRegistry.PROP_CONTENTS && source instanceof IEditorRegistry) { ! createFileExtensions() ; ! firePropertyChange(PROP_MATCH_CRITERIA) ; } } ! } ; /** ! * The set of files that are generally associated with Ruby, ! * but aren't editable by the RubyEditor (they're not ruby code) */ private static Set RUBY_NON_EDITABLE_EXTENSIONS = new HashSet(); static { RUBY_NON_EDITABLE_EXTENSIONS.add("yaml"); RUBY_NON_EDITABLE_EXTENSIONS.add("rhtml"); RUBY_NON_EDITABLE_EXTENSIONS.add("gem"); RUBY_NON_EDITABLE_EXTENSIONS.add("gemspec"); } ! public RubyFileMatcher() { ! propChangeListeners = new ListenerList() ; ! this.createFileExtensions() ; ! WorkbenchPlugin.getDefault().getEditorRegistry().addPropertyListener(propertyListener) ; } public void addPropertyChangeListener(IPropertyListener propListener) { ! propChangeListeners.add(propListener) ; } ! private void firePropertyChange(final int type) { Object[] array = propChangeListeners.getListeners(); --- 35,85 ---- /** * RubyViewerFilter uses the Editor mappings for recognising and filtering files ! * Both Editor mappings from plugin.xml and creating using the preferences page ! * are considered */ public class RubyFileMatcher { + + private static final String RUBY = "ruby"; + private static final String SHEBANG = "#!"; public static final int PROP_MATCH_CRITERIA = 1; ! private String[] rubyFileExtensions; ! private String[] rubyFileNames; ! private ListenerList propChangeListeners; ! private IPropertyListener propertyListener = new IPropertyListener() { ! public void propertyChanged(Object source, int property) { ! if (property == IEditorRegistry.PROP_CONTENTS ! && source instanceof IEditorRegistry) { ! createFileExtensions(); ! firePropertyChange(PROP_MATCH_CRITERIA); } } ! }; /** ! * The set of files that are generally associated with Ruby, but aren't ! * editable by the RubyEditor (they're not ruby code) */ private static Set RUBY_NON_EDITABLE_EXTENSIONS = new HashSet(); static { RUBY_NON_EDITABLE_EXTENSIONS.add("yaml"); + RUBY_NON_EDITABLE_EXTENSIONS.add("yml"); RUBY_NON_EDITABLE_EXTENSIONS.add("rhtml"); RUBY_NON_EDITABLE_EXTENSIONS.add("gem"); RUBY_NON_EDITABLE_EXTENSIONS.add("gemspec"); } ! public RubyFileMatcher() { ! propChangeListeners = new ListenerList(); ! this.createFileExtensions(); ! WorkbenchPlugin.getDefault().getEditorRegistry().addPropertyListener( ! propertyListener); } public void addPropertyChangeListener(IPropertyListener propListener) { ! propChangeListeners.add(propListener); } ! private void firePropertyChange(final int type) { Object[] array = propChangeListeners.getListeners(); *************** *** 81,90 **** } } ! public void createFileExtensions() { List extensions = new ArrayList(); extensions.addAll(createDefaultExtensions()); List filenames = new ArrayList(); ! IFileEditorMapping[] mappings = WorkbenchPlugin.getDefault().getEditorRegistry().getFileEditorMappings(); for (int i = 0; i < mappings.length; i++) { IFileEditorMapping mapping = mappings[i]; --- 93,104 ---- } } ! public void createFileExtensions() { List extensions = new ArrayList(); extensions.addAll(createDefaultExtensions()); List filenames = new ArrayList(); ! filenames.addAll(createDefaultFilenames()); ! IFileEditorMapping[] mappings = WorkbenchPlugin.getDefault() ! .getEditorRegistry().getFileEditorMappings(); for (int i = 0; i < mappings.length; i++) { IFileEditorMapping mapping = mappings[i]; *************** *** 94,104 **** if (descriptor.getId().equals(IRubyConstants.EDITOR_ID)) { // a mapping can also use a filename instead of a suffix ! if (mapping.getExtension() != null && mapping.getExtension().length() != 0) { extensions.add(mapping.getExtension()); break; } ! if (mapping.getName() != null && mapping.getName().length() != 0) { filenames.add(mapping.getName()); ! break ; } } --- 108,120 ---- if (descriptor.getId().equals(IRubyConstants.EDITOR_ID)) { // a mapping can also use a filename instead of a suffix ! if (mapping.getExtension() != null ! && mapping.getExtension().length() != 0) { extensions.add(mapping.getExtension()); break; } ! if (mapping.getName() != null ! && mapping.getName().length() != 0) { filenames.add(mapping.getName()); ! break; } } *************** *** 106,118 **** } } ! this.rubyFileExtensions = (String[]) extensions.toArray(new String[extensions.size()]); ! this.rubyFileNames = (String[]) filenames.toArray(new String[filenames.size()]); } ! private Collection createDefaultExtensions() { return RUBY_NON_EDITABLE_EXTENSIONS; } ! public boolean hasRubyEditorAssociation(IFile file) { String fileExtension = file.getFileExtension(); for (int i = 0; i < rubyFileExtensions.length; i++) { --- 122,152 ---- } } ! this.rubyFileExtensions = (String[]) extensions ! .toArray(new String[extensions.size()]); ! this.rubyFileNames = (String[]) filenames.toArray(new String[filenames ! .size()]); } ! ! /** ! * The default list of full filenames for ruby related files. ! * ! * @return a Collection of filenames associated with ruby projects. ! */ ! private Collection createDefaultFilenames() { ! Set set = new HashSet(); ! set.add("Rakefile"); ! return set; ! } ! ! /** ! * The default list of file extensions for ruby related files. ! * ! * @return a Collection of file extensions associated with ruby files. ! */ private Collection createDefaultExtensions() { return RUBY_NON_EDITABLE_EXTENSIONS; } ! public boolean hasRubyEditorAssociation(IFile file) { String fileExtension = file.getFileExtension(); for (int i = 0; i < rubyFileExtensions.length; i++) { *************** *** 121,125 **** } } ! String fileName = file.getName() ; for (int i = 0; i < rubyFileNames.length; i++) { if (rubyFileNames[i].equalsIgnoreCase(fileName)) { --- 155,159 ---- } } ! String fileName = file.getName(); for (int i = 0; i < rubyFileNames.length; i++) { if (rubyFileNames[i].equalsIgnoreCase(fileName)) { *************** *** 127,137 **** } } ! return false; } ! ! public boolean isRubyRunFile(IFile file) { ! return this.hasRubyEditorAssociation(file) ; } - } - --- 161,198 ---- } } ! return containsRubyShebang(file); } ! ! /** ! * Read the first line and check for '#!' and 'ruby' If we find them, assume ! * this is a ruby file. ! * ! * @param file ! * The file to check ! * @return ! */ ! private boolean containsRubyShebang(IFile file) { ! BufferedReader reader = null; ! try { ! reader = new BufferedReader(new InputStreamReader(file ! .getContents())); ! String firstLine = reader.readLine(); ! if (firstLine == null) ! return false; ! if (firstLine.contains(SHEBANG) && firstLine.contains(RUBY)) ! return true; ! } catch (CoreException e) { ! e.printStackTrace(); ! } catch (IOException e) { ! e.printStackTrace(); ! } finally { ! try { ! if (reader != null) ! reader.close(); ! } catch (IOException e) { ! e.printStackTrace(); ! } ! } ! return false; } } |