diff options
author | Juergen Haug | 2014-04-07 08:47:12 +0000 |
---|---|---|
committer | Juergen Haug | 2014-04-07 10:18:58 +0000 |
commit | c42d43a82981e59872af63d215bcc52fe5147859 (patch) | |
tree | df8948f69dee852759949d5bf5765792efeee705 | |
parent | 7d7132b2595d7b4ca7719d1c17144ff0d6a6bd59 (diff) | |
download | org.eclipse.etrice-c42d43a82981e59872af63d215bcc52fe5147859.tar.gz org.eclipse.etrice-c42d43a82981e59872af63d215bcc52fe5147859.tar.xz org.eclipse.etrice-c42d43a82981e59872af63d215bcc52fe5147859.zip |
[ui] quickfix for multi line detail code string
Change-Id: I4a850c9e43ca445cea1ae859cf89e19dceba47e3
6 files changed, 182 insertions, 0 deletions
diff --git a/plugins/org.eclipse.etrice.core.room.ui/.classpath b/plugins/org.eclipse.etrice.core.room.ui/.classpath index bcc32a048..7fee52fc9 100644 --- a/plugins/org.eclipse.etrice.core.room.ui/.classpath +++ b/plugins/org.eclipse.etrice.core.room.ui/.classpath @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> + <classpathentry kind="src" path="xtend-gen"/> <classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src-gen"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> diff --git a/plugins/org.eclipse.etrice.core.room.ui/.project b/plugins/org.eclipse.etrice.core.room.ui/.project index 517f4a90f..2ff7491d4 100644 --- a/plugins/org.eclipse.etrice.core.room.ui/.project +++ b/plugins/org.eclipse.etrice.core.room.ui/.project @@ -6,6 +6,11 @@ </projects> <buildSpec> <buildCommand> + <name>org.eclipse.xtext.ui.shared.xtextBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> @@ -24,5 +29,6 @@ <natures> <nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.xtext.ui.shared.xtextNature</nature> </natures> </projectDescription> diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/DetailCodeIndentHelper.xtend b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/DetailCodeIndentHelper.xtend new file mode 100644 index 000000000..f6bd698aa --- /dev/null +++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/DetailCodeIndentHelper.xtend @@ -0,0 +1,50 @@ +package org.eclipse.etrice.core.ui.quickfix + +import java.util.ArrayList +import org.eclipse.xtext.ui.editor.model.IXtextDocument +import org.eclipse.xtext.util.Strings +import org.eclipse.xtext.validation.Issue + +class DetailCodeIndentHelper { + + /** + * Converts the issued multi line string to single line strings + * - line with least indentation is adjusted to first line + * - lines are enclosed with " or ' + * - lines get editor indentation of first line + * - + */ + def static convertToSingleLines(IXtextDocument document, Issue issue) { + val issuedRegion = document.getLineInformationOfOffset(issue.getOffset()) // note: other methods return wrong line + + val firstEditorLine = document.get(issuedRegion.getOffset(), issuedRegion.getLength()) + val editorIndent = Strings.getLeadingWhiteSpace(firstEditorLine) + + val editorString = document.get(issue.getOffset(), issue.getLength()) + val mark = editorString.charAt(0).toString // " or ' + val editorLines = Strings::split(editorString, Strings.newLine()) + + //val initialIndent = editorLines.head.countIndent + val minIndent = editorLines.tail.sortBy[countIndent()].head.countIndent + + val newEditorLines = new ArrayList<String>() + editorLines.forEach [ line, lineCount | + var front = if(lineCount != 0) editorIndent + mark + var indentCount = line.countIndent - minIndent + var indent = if (lineCount != 0 && indentCount > 0) '''«FOR i : (1 .. indentCount)» «ENDFOR»''' // \t + var noneWSLine = Strings::removeLeadingWhitespace(line) + var end = if(lineCount != editorLines.size - 1) mark + var newEditorLine = '''«front»«indent»«noneWSLine»«end»''' + newEditorLines.add(newEditorLine) + ] + + return Strings::concat(Strings.newLine(), newEditorLines) + } + + private static def countIndent(String line) { + // simplistic whitespace handling, drop spaces etc. + // first convert 4x spaces to 1 indent, after that remove everything else + Strings::getLeadingWhiteSpace(line).replace("\t", " ").replace(" ", "\t").replaceAll("!\\t","").length + + } +} diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java index e8583b53b..92e33a7bd 100644 --- a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java +++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java @@ -317,5 +317,18 @@ public class RoomQuickfixProvider extends DefaultQuickfixProvider { }); } + + + @Fix(RoomJavaValidator.MULTI_LINE_DETAILCODE) + public void fixMultiLineDetailCode(final Issue issue, IssueResolutionAcceptor acceptor){ + acceptor.accept(issue, "Convert to single lines", "", "correction_change.gif", new IModification() { + @Override + public void apply(IModificationContext context) throws Exception { + IXtextDocument document = context.getXtextDocument(); + String editorReplace = DetailCodeIndentHelper.convertToSingleLines(document, issue); + document.replace(issue.getOffset(), issue.getLength(), editorReplace); + } + }); + } } diff --git a/plugins/org.eclipse.etrice.core.room.ui/xtend-gen/org/eclipse/etrice/core/ui/quickfix/DetailCodeIndentHelper.java b/plugins/org.eclipse.etrice.core.room.ui/xtend-gen/org/eclipse/etrice/core/ui/quickfix/DetailCodeIndentHelper.java new file mode 100644 index 000000000..e3ea3bc72 --- /dev/null +++ b/plugins/org.eclipse.etrice.core.room.ui/xtend-gen/org/eclipse/etrice/core/ui/quickfix/DetailCodeIndentHelper.java @@ -0,0 +1,104 @@ +package org.eclipse.etrice.core.ui.quickfix; + +import java.util.ArrayList; +import java.util.List; +import org.eclipse.jface.text.IRegion; +import org.eclipse.xtend2.lib.StringConcatenation; +import org.eclipse.xtext.ui.editor.model.IXtextDocument; +import org.eclipse.xtext.util.Strings; +import org.eclipse.xtext.validation.Issue; +import org.eclipse.xtext.xbase.lib.Exceptions; +import org.eclipse.xtext.xbase.lib.Functions.Function1; +import org.eclipse.xtext.xbase.lib.IntegerRange; +import org.eclipse.xtext.xbase.lib.IterableExtensions; +import org.eclipse.xtext.xbase.lib.Procedures.Procedure2; + +@SuppressWarnings("all") +public class DetailCodeIndentHelper { + /** + * Converts the issued multi line string to single line strings + * - line with least indentation is adjusted to first line + * - lines are enclosed with " or ' + * - lines get editor indentation of first line + * - + */ + public static String convertToSingleLines(final IXtextDocument document, final Issue issue) { + try { + Integer _offset = issue.getOffset(); + final IRegion issuedRegion = document.getLineInformationOfOffset((_offset).intValue()); + int _offset_1 = issuedRegion.getOffset(); + int _length = issuedRegion.getLength(); + final String firstEditorLine = document.get(_offset_1, _length); + final String editorIndent = Strings.getLeadingWhiteSpace(firstEditorLine); + Integer _offset_2 = issue.getOffset(); + Integer _length_1 = issue.getLength(); + final String editorString = document.get((_offset_2).intValue(), (_length_1).intValue()); + char _charAt = editorString.charAt(0); + final String mark = Character.valueOf(_charAt).toString(); + String _newLine = Strings.newLine(); + final List<String> editorLines = Strings.split(editorString, _newLine); + Iterable<String> _tail = IterableExtensions.<String>tail(editorLines); + final Function1<String,Integer> _function = new Function1<String,Integer>() { + public Integer apply(final String it) { + return Integer.valueOf(DetailCodeIndentHelper.countIndent(it)); + } + }; + List<String> _sortBy = IterableExtensions.<String, Integer>sortBy(_tail, _function); + String _head = IterableExtensions.<String>head(_sortBy); + final int minIndent = DetailCodeIndentHelper.countIndent(_head); + final ArrayList<String> newEditorLines = new ArrayList<String>(); + final Procedure2<String,Integer> _function_1 = new Procedure2<String,Integer>() { + public void apply(final String line, final Integer lineCount) { + String _xifexpression = null; + if (((lineCount).intValue() != 0)) { + _xifexpression = (editorIndent + mark); + } + String front = _xifexpression; + int _countIndent = DetailCodeIndentHelper.countIndent(line); + int indentCount = (_countIndent - minIndent); + String _xifexpression_1 = null; + if ((((lineCount).intValue() != 0) && (indentCount > 0))) { + StringConcatenation _builder = new StringConcatenation(); + { + IntegerRange _upTo = new IntegerRange(1, indentCount); + for(final Integer i : _upTo) { + _builder.append("\t"); + } + } + _xifexpression_1 = _builder.toString(); + } + String indent = _xifexpression_1; + String noneWSLine = Strings.removeLeadingWhitespace(line); + String _xifexpression_2 = null; + int _size = editorLines.size(); + int _minus = (_size - 1); + boolean _notEquals = ((lineCount).intValue() != _minus); + if (_notEquals) { + _xifexpression_2 = mark; + } + String end = _xifexpression_2; + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append(front, ""); + _builder_1.append(indent, ""); + _builder_1.append(noneWSLine, ""); + _builder_1.append(end, ""); + String newEditorLine = _builder_1.toString(); + newEditorLines.add(newEditorLine); + } + }; + IterableExtensions.<String>forEach(editorLines, _function_1); + String _newLine_1 = Strings.newLine(); + return Strings.concat(_newLine_1, newEditorLines); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + private static int countIndent(final String line) { + String _leadingWhiteSpace = Strings.getLeadingWhiteSpace(line); + String _replace = _leadingWhiteSpace.replace("\t", " "); + String _replace_1 = _replace.replace(" ", "\t"); + String _replaceAll = _replace_1.replaceAll("!\\t", ""); + return _replaceAll.length(); + } +} diff --git a/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java b/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java index dc7b2fa7d..0f0e089f8 100644 --- a/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java +++ b/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java @@ -77,6 +77,7 @@ import org.eclipse.etrice.core.room.Transition; import org.eclipse.etrice.core.room.util.RoomHelpers; import org.eclipse.etrice.core.validation.ValidationUtil.Result; import org.eclipse.xtext.scoping.impl.ImportUriResolver; +import org.eclipse.xtext.util.Strings; import org.eclipse.xtext.validation.Check; import com.google.inject.Inject; @@ -100,6 +101,7 @@ public class RoomJavaValidator extends AbstractRoomJavaValidator { public static final String CHANGE_DESTRUCTOR_NAME = "RoomJavaValidator.ChangeDestructorName"; public static final String CHANGE_CONSTRUCTOR_NAME = "RoomJavaValidator.ChangeConstructorName"; public static final String INVALID_ANNOTATION_TARGET = "RoomJavaValidator.InvalidAnnotationTarget"; + public static final String MULTI_LINE_DETAILCODE = "RoomJavaValidator.MultiLineDetailCode"; @Inject ImportUriResolver importUriResolver; @@ -731,6 +733,12 @@ public class RoomJavaValidator extends AbstractRoomJavaValidator { public void checkDetailCode(DetailCode dc) { if (dc.getLines().isEmpty()) error("detail code must not be empty", dc, RoomPackage.Literals.DETAIL_CODE__LINES); + + for(String line : dc.getLines()){ + // bad: "\r\n" is affected too + if(line.contains(Strings.newLine())) + warning("multi line string", dc, RoomPackage.Literals.DETAIL_CODE__LINES, dc.getLines().indexOf(line), MULTI_LINE_DETAILCODE); + } } @Check |