package org.rubypeople.rdt.internal.corext.codemanipulation;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.rubypeople.rdt.core.IOpenable;
import org.rubypeople.rdt.core.IRubyElement;
import org.rubypeople.rdt.core.IRubyProject;
import org.rubypeople.rdt.core.RubyModelException;
public class StubUtility {
/**
* Returns the line delimiter which is used in the specified project.
*
* @param project the ruby project, or <code>null</code>
* @return the used line delimiter
*/
public static String getLineDelimiterUsed(IRubyProject project) {
return getProjectLineDelimiter(project);
}
private static String getProjectLineDelimiter(IRubyProject javaProject) {
IProject project= null;
if (javaProject != null)
project= javaProject.getProject();
String lineDelimiter= getLineDelimiterPreference(project);
if (lineDelimiter != null)
return lineDelimiter;
return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
}
public static String getLineDelimiterPreference(IProject project) {
IScopeContext[] scopeContext;
if (project != null) {
// project preference
scopeContext= new IScopeContext[] { new ProjectScope(project) };
String lineDelimiter= Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext);
if (lineDelimiter != null)
return lineDelimiter;
}
// workspace preference
scopeContext= new IScopeContext[] { new InstanceScope() };
String platformDefault= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, platformDefault, scopeContext);
}
/**
* Examines a string and returns the first line delimiter found.
*/
public static String getLineDelimiterUsed(IRubyElement elem) {
while (elem != null && !(elem instanceof IOpenable)) {
elem= elem.getParent();
}
if (elem != null) {
try {
return ((IOpenable) elem).findRecommendedLineSeparator();
} catch (RubyModelException exception) {
// Use project setting
}
}
return getProjectLineDelimiter(null);
}
}