Java Code Examples for org.eclipse.xtext.util.Strings#newLine()

The following examples show how to use org.eclipse.xtext.util.Strings#newLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportRewriter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private ImportRewriter(IDocument document, Resource resource) {
	if (document instanceof IDocumentExtension4) {
		lineDelimiter = ((IDocumentExtension4) document).getDefaultLineDelimiter();
	} else {
		lineDelimiter = Strings.newLine();
	}
	this.script = (Script) resource.getContents().get(0);
	this.existingImports = Lists.newArrayList();
	for (ScriptElement element : script.getScriptElements()) {
		if (element instanceof ImportDeclaration)
			existingImports.add((ImportDeclaration) element);
	}
	this.requestedImports = Sets.newLinkedHashSet();
	this.lazySpacer = new Lazy<>(() -> spacerPreference.getSpacingPreference(resource));

}
 
Example 2
Source File: ManifestMerger2Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testNoLongLine() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest2 manifest = new MergeableManifest2(resourceAsStream);
	manifest.addExportedPackages(Collections.singleton("foobar"));
	assertTrue(manifest.isModified());
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	manifest.write(out);
	String result = out.toString();
	String lookup = "Require-Bundle: org.eclipse.xtext,";
	int idx = result.indexOf(lookup);
	assertTrue(idx != -1);
	idx += lookup.length();
	String lineDelimiter = Strings.newLine();
	for (int i = 0; i < lineDelimiter.length(); i++) {
		assertEquals(result, lineDelimiter.charAt(i), result.charAt(idx + i));
	}
	assertEquals(result, ' ', result.charAt(idx + lineDelimiter.length()));
}
 
Example 3
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNoLongLine() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest manifest = new MergeableManifest(resourceAsStream);
	manifest.addExportedPackages(Collections.singleton("foobar"));
	assertTrue(manifest.isModified());
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	manifest.write(out);
	String result = out.toString();
	String lookup = "Require-Bundle: org.eclipse.xtext,";
	int idx = result.indexOf(lookup);
	assertTrue(idx != -1);
	idx += lookup.length();
	String lineDelimiter = Strings.newLine();
	for (int i=0; i< lineDelimiter.length(); i++) {
		assertEquals(result, lineDelimiter.charAt(i), result.charAt(idx+i));
	}
	assertEquals(result, ' ', result.charAt(idx + lineDelimiter.length()));
}
 
Example 4
Source File: EMFGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
void updateBuildProperties(XpandExecutionContext ctx) throws Exception {
	if (!updateBuildProperties || modelPluginID != null)
		return;
	Outlet rootOutlet = ctx.getOutput().getOutlet(org.eclipse.xtext.generator.Generator.PLUGIN_RT);
	Outlet modelOutlet = ctx.getOutput().getOutlet(org.eclipse.xtext.generator.Generator.MODEL);
	String buildPropertiesPath = rootOutlet.getPath() + "/build.properties";
	String modelPath = modelOutlet.getPath().substring(rootOutlet.getPath().length() + 1) + "/";
	Properties buildProperties = new Properties();
	Reader reader = new InputStreamReader(new FileInputStream(new File(buildPropertiesPath)), Charset.forName(rootOutlet.getFileEncoding()));
	try {
		String existingContent = CharStreams.toString(reader);
		// for encoding details, see Properties.load
		buildProperties.load(new StringInputStream(existingContent, "ISO-8859-1"));
		String binIncludes = buildProperties.getProperty("bin.includes");
		boolean changed = false;
		if (binIncludes == null) {
			existingContent += "bin.includes = " + modelPath + Strings.newLine()+ "               ";
			changed = true;
		} else if (!binIncludes.contains(modelPath)) {
			existingContent = existingContent.replace("bin.includes = ", "bin.includes = " + modelPath + ",\\" + Strings.newLine() +"               ");
			changed = true;
		}
		if (changed) {
			Writer writer = new OutputStreamWriter(new FileOutputStream(new File(buildPropertiesPath)), Charset.forName(rootOutlet.getFileEncoding()));
			writer.write(existingContent);
			writer.close();
		}
	} finally {
		reader.close();
	}
}
 
Example 5
Source File: XbaseFormatterTester.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void assertFormattedExpression(Procedure1<? super FormatterTestRequest> test) {
	String prefix = "{" + Strings.newLine();
	String postfix = Strings.newLine() + "}";
	assertFormatted((FormatterTestRequest it) -> {
		test.apply(it);
		it.preferences((MapBasedPreferenceValues prefs) -> {
			prefs.put(FormatterPreferenceKeys.maxLineWidth, 80);
		});
		it.setExpectation(prefix + indent(it.getExpectationOrToBeFormatted().toString().trim(), "\t") + postfix);
		it.setToBeFormatted(prefix + indent(it.getToBeFormatted().toString().trim(), "\t") + postfix);
	});
}
 
Example 6
Source File: WhitespaceHelperTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testInsertLines_0() throws Exception {
	String doubleNewLine = Strings.newLine() + Strings.newLine();
	WhitespaceHelper w = getWhitespaceHelper("xxx|yyy", 0, true, "xxx" + doubleNewLine + "*" + doubleNewLine + "yyy");
	assertEquals(3, w.getTotalOffset());
	assertEquals(0, w.getTotalLength());
	assertEquals(doubleNewLine, w.getPrefix());
	assertEquals(doubleNewLine, w.getSuffix());
}
 
Example 7
Source File: WhitespaceHelperTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testInsertLines_5() throws Exception {
	String doubleNewLine = Strings.newLine() + Strings.newLine();
	WhitespaceHelper w = getWhitespaceHelper("xxx | yyy", 0, true, "xxx "+ doubleNewLine + "*" + doubleNewLine + "yyy");
	assertEquals(4, w.getTotalOffset());
	assertEquals(1, w.getTotalLength());
	assertEquals(doubleNewLine, w.getPrefix());
	assertEquals(doubleNewLine, w.getSuffix());
}
 
Example 8
Source File: JavaConverterTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRichStringCase1() throws Exception {
  String _newLine = Strings.newLine();
  String _plus = ("int i=0" + _newLine);
  String _plus_1 = (_plus + "String richTxt=\'\'\'int i=�i�.\'\'\'");
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("private int i = 0;");
  _builder.newLine();
  _builder.append("private String richTxt = \"int \"+\"i=\"+i+\".\";");
  _builder.newLine();
  Assert.assertEquals(_plus_1, this.toXtendClassBodyDeclr(_builder));
}
 
Example 9
Source File: EMFGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private void updateBuildProperties() {
  try {
    if ((((!this.updateBuildProperties) || (this.modelPluginID != null)) || (this.getProjectConfig().getRuntime().getManifest() == null))) {
      return;
    }
    String _path = this.getProjectConfig().getRuntime().getRoot().getPath();
    final String buildPropertiesPath = (_path + "/build.properties");
    final File buildPropertiesFile = new File(buildPropertiesPath);
    boolean _exists = buildPropertiesFile.exists();
    if (_exists) {
      final String modelContainer = this.getProjectConfig().getRuntime().getEcoreModelFolder();
      final Properties buildProperties = new Properties();
      final Charset charset = Charset.forName(this.codeConfig.getEncoding());
      FileInputStream _fileInputStream = new FileInputStream(buildPropertiesFile);
      final InputStreamReader reader = new InputStreamReader(_fileInputStream, charset);
      String _xtrycatchfinallyexpression = null;
      try {
        _xtrycatchfinallyexpression = CharStreams.toString(reader);
      } finally {
        reader.close();
      }
      String existingContent = _xtrycatchfinallyexpression;
      StringInputStream _stringInputStream = new StringInputStream(existingContent, "ISO-8859-1");
      buildProperties.load(_stringInputStream);
      final String binIncludes = buildProperties.getProperty("bin.includes");
      boolean changed = false;
      if ((binIncludes == null)) {
        String _existingContent = existingContent;
        String _newLine = Strings.newLine();
        String _plus = ((("bin.includes = " + modelContainer) + "/") + _newLine);
        String _plus_1 = (_plus + "               ");
        existingContent = (_existingContent + _plus_1);
        changed = true;
      } else {
        boolean _contains = binIncludes.contains(modelContainer);
        boolean _not = (!_contains);
        if (_not) {
          String _newLine_1 = Strings.newLine();
          String _plus_2 = ((("bin.includes = " + modelContainer) + "/,\\") + _newLine_1);
          String _plus_3 = (_plus_2 + "               ");
          existingContent = existingContent.replace("bin.includes = ", _plus_3);
          changed = true;
        }
      }
      if (changed) {
        FileOutputStream _fileOutputStream = new FileOutputStream(buildPropertiesFile);
        final OutputStreamWriter writer = new OutputStreamWriter(_fileOutputStream, charset);
        try {
          writer.write(existingContent);
        } finally {
          writer.close();
        }
      }
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}