diff options
author | Juergen Haug | 2017-08-29 14:08:52 +0000 |
---|---|---|
committer | Juergen Haug | 2017-08-29 14:55:38 +0000 |
commit | c72ab314a29234cf980b85d12c18734747ee2176 (patch) | |
tree | 0183a601bcc04af0be62a88bbb81813b54e10251 /plugins/org.eclipse.etrice.core.common | |
parent | 965e852cc5dc0a3a6891c5bd8962d596eb122238 (diff) | |
download | org.eclipse.etrice-c72ab314a29234cf980b85d12c18734747ee2176.tar.gz org.eclipse.etrice-c72ab314a29234cf980b85d12c18734747ee2176.tar.xz org.eclipse.etrice-c72ab314a29234cf980b85d12c18734747ee2176.zip |
[targetLang] improved reusability, switched to new xtext highlight api
Change-Id: I1eee0f3cc15fe60bbe1f302ed7b68edf29b2f527
Diffstat (limited to 'plugins/org.eclipse.etrice.core.common')
5 files changed, 180 insertions, 43 deletions
diff --git a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/BaseConverterService.java b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/BaseConverterService.java index 2a7778e59..1400f15df 100644 --- a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/BaseConverterService.java +++ b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/BaseConverterService.java @@ -26,7 +26,7 @@ public class BaseConverterService extends DefaultTerminalConverters { private TimeConverter timeConverter = new TimeConverter(); private LongConverter longConverter = new LongConverter(); private DoubleConverter doubleConverter = new DoubleConverter(); - private CC_StringConverter ccStringConverter = new CC_StringConverter(); + private CCStringConverter ccStringConverter = new CCStringConverter("'''"); @Inject private QualifiedNameValueConverter fqnConverter; @@ -52,7 +52,8 @@ public class BaseConverterService extends DefaultTerminalConverters { } @ValueConverter(rule = "CC_STRING") - public IValueConverter<String> getCC_StringConverter() { + public CCStringConverter getCC_StringConverter() { return ccStringConverter; } + } diff --git a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/CCStringConverter.java b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/CCStringConverter.java new file mode 100644 index 000000000..2a9476e87 --- /dev/null +++ b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/CCStringConverter.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2011 protos software gmbh (http://www.protos.de). + * 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 + * + * CONTRIBUTORS: + * Juergen Haug (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.core.common.converter; + +import org.eclipse.xtext.conversion.ValueConverterException; +import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter; +import org.eclipse.xtext.nodemodel.INode; + +public class CCStringConverter extends AbstractLexerBasedConverter<String> { + + static public String addDelim(final String text, String delim) { + if(CCStringIndentation.hasLineBreak(text)) { + final String lineEnding = CCStringIndentation.getLineEnding(text); + return delim + lineEnding + text + delim; + } + return delim + text + delim; + } + + static public String stripDelim(String nodeText, String delim) { + if (nodeText.startsWith(delim) && nodeText.endsWith(delim)) { + return nodeText.substring(delim.length(), nodeText.length() - delim.length()); + } + + return nodeText; + } + + private final String delim; + + public CCStringConverter(String delim) { + this.delim = delim; + } + + public String getDelim() { + return delim; + } + + @Override + protected String toEscapedString(String value) { + return addDelim(value, delim); + } + + public String toValue(String string, INode node) { + if (string == null) + return null; + try { + return new CCStringIndentation(stripDelim(string, delim)).removeEditorWhiteSpace(); + } catch (IllegalArgumentException e) { + throw new ValueConverterException(e.getMessage(), node, e); + } + } + + public String addDelim(String text) { + return addDelim(text, delim); + } + + public String stripDelim(String text) { + return stripDelim(text, delim); + } +} diff --git a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/CC_StringConverter.java b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/CC_StringConverter.java deleted file mode 100644 index 703a6a92d..000000000 --- a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/converter/CC_StringConverter.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.eclipse.etrice.core.common.converter; - -import org.eclipse.xtext.conversion.ValueConverterException; -import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter; -import org.eclipse.xtext.nodemodel.INode; - -public class CC_StringConverter extends AbstractLexerBasedConverter<String> { - - static public String DELIM = "'''"; - - static public String addDelim(final String text) { - if(CCStringIndentation.hasLineBreak(text)) { - final String lineEnding = CCStringIndentation.getLineEnding(text); - return DELIM + lineEnding + text + DELIM; - } - return DELIM + text + DELIM; - } - - static public String stripDelim(String nodeText) { - if (nodeText.startsWith(DELIM) && nodeText.endsWith(DELIM)) { - return nodeText.substring(DELIM.length(), nodeText.length() - DELIM.length()); - } - - return nodeText; - } - - @Override - protected String toEscapedString(String value) { - return addDelim(value); - } - - public String toValue(String string, INode node) { - if (string == null) - return null; - try { - return new CCStringIndentation(stripDelim(string)).removeEditorWhiteSpace(); - } catch (IllegalArgumentException e) { - throw new ValueConverterException(e.getMessage(), node, e); - } - } -} diff --git a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/formatting2/CCStringReplacer.xtend b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/formatting2/CCStringReplacer.xtend new file mode 100644 index 000000000..6f2ede0e9 --- /dev/null +++ b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/formatting2/CCStringReplacer.xtend @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2011 protos software gmbh (http://www.protos.de). + * 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 + * + * CONTRIBUTORS: + * Juergen Haug (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.core.common.formatting2 + +import org.eclipse.etrice.core.common.converter.CCStringConverter +import org.eclipse.etrice.core.common.converter.CCStringIndentation +import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor +import org.eclipse.xtext.formatting2.ITextReplacerContext +import org.eclipse.xtext.formatting2.internal.AbstractTextReplacer + +@FinalFieldsConstructor +class CCStringReplacer extends AbstractTextReplacer { + + val CCStringConverter converter + + override createReplacements(ITextReplacerContext context) { + if (region.multiline) { + val ccIndent = new CCStringIndentation(converter.stripDelim(region.text.trim)) + val endIndent = if(ccIndent.ignoreLast) context.indentationString else '' + val replacement = ccIndent.replaceEditorIndentation(context.getIndentationString(context.indentation + 1)) + + endIndent + context => [ + addReplacement(region.replaceWith(converter.delim + replacement + converter.delim)) + ] + } + return context + } +} diff --git a/plugins/org.eclipse.etrice.core.common/xtend-gen/org/eclipse/etrice/core/common/formatting2/CCStringReplacer.java b/plugins/org.eclipse.etrice.core.common/xtend-gen/org/eclipse/etrice/core/common/formatting2/CCStringReplacer.java new file mode 100644 index 000000000..87dba2255 --- /dev/null +++ b/plugins/org.eclipse.etrice.core.common/xtend-gen/org/eclipse/etrice/core/common/formatting2/CCStringReplacer.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2011 protos software gmbh (http://www.protos.de). + * 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 + * + * CONTRIBUTORS: + * Juergen Haug (initial contribution) + */ +package org.eclipse.etrice.core.common.formatting2; + +import org.eclipse.etrice.core.common.converter.CCStringConverter; +import org.eclipse.etrice.core.common.converter.CCStringIndentation; +import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor; +import org.eclipse.xtext.formatting2.IFormattableDocument; +import org.eclipse.xtext.formatting2.ITextReplacerContext; +import org.eclipse.xtext.formatting2.internal.AbstractTextReplacer; +import org.eclipse.xtext.formatting2.regionaccess.ITextReplacement; +import org.eclipse.xtext.formatting2.regionaccess.ITextSegment; +import org.eclipse.xtext.xbase.lib.ObjectExtensions; +import org.eclipse.xtext.xbase.lib.Procedures.Procedure1; + +@FinalFieldsConstructor +@SuppressWarnings("all") +public class CCStringReplacer extends AbstractTextReplacer { + private final CCStringConverter converter; + + @Override + public ITextReplacerContext createReplacements(final ITextReplacerContext context) { + ITextSegment _region = this.getRegion(); + boolean _isMultiline = _region.isMultiline(); + if (_isMultiline) { + ITextSegment _region_1 = this.getRegion(); + String _text = _region_1.getText(); + String _trim = _text.trim(); + String _stripDelim = this.converter.stripDelim(_trim); + final CCStringIndentation ccIndent = new CCStringIndentation(_stripDelim); + String _xifexpression = null; + boolean _isIgnoreLast = ccIndent.isIgnoreLast(); + if (_isIgnoreLast) { + _xifexpression = context.getIndentationString(); + } else { + _xifexpression = ""; + } + final String endIndent = _xifexpression; + int _indentation = context.getIndentation(); + int _plus = (_indentation + 1); + String _indentationString = context.getIndentationString(_plus); + String _replaceEditorIndentation = ccIndent.replaceEditorIndentation(_indentationString); + final String replacement = (_replaceEditorIndentation + endIndent); + final Procedure1<ITextReplacerContext> _function = (ITextReplacerContext it) -> { + ITextSegment _region_2 = this.getRegion(); + String _delim = this.converter.getDelim(); + String _plus_1 = (_delim + replacement); + String _delim_1 = this.converter.getDelim(); + String _plus_2 = (_plus_1 + _delim_1); + ITextReplacement _replaceWith = _region_2.replaceWith(_plus_2); + it.addReplacement(_replaceWith); + }; + ObjectExtensions.<ITextReplacerContext>operator_doubleArrow(context, _function); + } + return context; + } + + public CCStringReplacer(final IFormattableDocument document, final ITextSegment region, final CCStringConverter converter) { + super(document, region); + this.converter = converter; + } +} |