Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.editor.formatting;
  
 import java.io.Reader;
 
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.core.resources.IWorkspace;
 import  org.eclipse.core.resources.ProjectScope;
 import  org.eclipse.core.runtime.Path;
 import  org.eclipse.core.runtime.Platform;
 import  org.eclipse.core.runtime.preferences.IScopeContext;
 import  org.eclipse.core.runtime.preferences.InstanceScope;
 

Author(s):
Jan Koehnlein - Initial contribution and API
Since:
2.3
 
 
 
 
 	private IWorkspace workspace;
 
 		return ;
 	}
 
 		final String lineSeparator = getLineSeparatorPreference(uri);
 		return new ILineSeparatorInformation() {
 			public String getLineSeparator() {
 				return lineSeparator;
 			}
 		};
 	}
 
 	protected String getLineSeparatorPreference(URI uri) {
 		if (uri.isPlatformResource()) {
 			IFile file = .getRoot().getFile(new Path(uri.toPlatformString(true)));
 			String delimiter = senseLineDelimiter(file);
 			if (delimiter != nullreturn delimiter;
 		}
 		IProject project = null;
 		if (uri.isPlatformResource()) {
 			project = .getRoot().getProject(uri.segment(1));
 		} else {
 			for (Pair<IStorage, IProject> storage : .getStorages(uri)) {
 				project = storage.getSecond();
 				break;
 			}
 		}
 		if (project != null) {
 			String result = getLineSeparatorPreference(new ProjectScope(project));
 			if (result != null)
 				return result;
 		}
 		String result = getLineSeparatorPreference(new InstanceScope());
 		if (result != null)
 			return result;
 		return System.getProperty("line.separator");
 	}
 	
 	private String senseLineDelimiter(IFile file) {
 		if (file.exists()) {
 			InputStream content = null;
 			try {
 				content = file.getContents();
 				Reader reader = new InputStreamReader(contentfile.getCharset());
 				int read;
 				boolean rFound = false;
				while ((read = reader.read()) != -1) {
					char c = (charread;
					if (c == '\n') {
						if (rFound) {
							return "\r\n";
else {
							return "\n";
						}
					}
					if (rFound) {
						return "\r";
					}
					if (c == '\r') {
						rFound = true;
					}
				}
				if (rFound) {
					return "\r";
				}
catch (Exception e) {
				throw new RuntimeIOException(e);
finally {
				try {
					if (content != null) {
						content.close();
					}
catch (IOException e) {
					throw new RuntimeIOException(e);
				}
			}
		}
		return null;
	}
	protected String getLineSeparatorPreference(IScopeContext scopeContext) {
		String lineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME,
				Platform.PREF_LINE_SEPARATOR, nullnew IScopeContext[] { scopeContext });
		return lineDelimiter;
	}
New to GrepCode? Check out our FAQ X