Skip to content

Commit

Permalink
[NONE] DOT Editor - Fixing BadLocationException in DotProposalProvider.
Browse files Browse the repository at this point in the history
- Modify the content assist proposal calculation logic to avoid throwing
org.eclipse.jface.text.BadLocationException.
- Implement corresponding DotContentAssistTest test cases.
  • Loading branch information
miklossy committed Jan 19, 2021
1 parent 99e2abf commit 45890d5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2020 itemis AG and others.
* Copyright (c) 2016, 2021 itemis AG and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -3019,6 +3019,19 @@ class DotContentAssistTest extends AbstractContentAssistTest {
}
''')

'''
graph {
1[color="blue"«c»]
}
'''.testContentAssistant(#[",", ";", "]", "color", "colorscheme", "distortion", "fillcolor", "fixedsize", "fontcolor", "fontname", "fontsize",
"height", "id", "label", "penwidth", "pos", "shape", "sides", "skew", "style", "tooltip", "width",
"xlabel", "xlp"], "fixedsize",
'''
graph {
1[color="blue"fixedsize=]
}
''')

// test local attribute names with prefix
'''
graph {
Expand Down Expand Up @@ -3054,6 +3067,16 @@ class DotContentAssistTest extends AbstractContentAssistTest {
}
''')

'''
graph {
1[ color=«c»"" ]
}
'''.testContentAssistant(combine(expectedX11ColorNames, #["#", "/"]), "#", '''
graph {
1[ color=#"" ]
}
''')

// test local attribute values with quotes
'''
graph {
Expand Down Expand Up @@ -3458,6 +3481,18 @@ class DotContentAssistTest extends AbstractContentAssistTest {
''')

// test html-like label attribute
'''
graph {
1[ label=«c»<> ]
}
'''.testContentAssistant(#["HTMLLabel - Insert a template"], "HTMLLabel - Insert a template", '''
graph {
1[ label=<
><> ]
}
''')

'''
graph {
1[ label=<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*******************************************************************************/
package org.eclipse.gef.dot.internal.ui.language.contentassist;

import static org.eclipse.gef.dot.internal.language.dot.DotPackage.Literals.ATTRIBUTE__VALUE;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand Down Expand Up @@ -69,6 +71,7 @@
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.ui.IImageHelper;
import org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal;
import org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal.IReplacementTextApplier;
Expand Down Expand Up @@ -313,6 +316,28 @@ public void completeAttribute_Value(EObject model, Assignment assignment,
.setMatcher(new AttributeValueMatcher(context.getMatcher()))
.toContext();

/*
* do not propose any attribute values if it is already properly
* enclosed with double quotes and the cursor is located directly
* after the closing quote
*/
ID attributeValue = attribute.getValue();
if (attributeValue != null) {
String attributeValueText = attributeValue.toString();
int cursorPosition = context.getOffset();

int attributeValueEndOffset = NodeModelUtils
.findNodesForFeature(attribute, ATTRIBUTE__VALUE).get(0)
.getTotalEndOffset();

if (attributeValueText.startsWith("\"") //$NON-NLS-1$
&& attributeValueText.endsWith("\"") //$NON-NLS-1$
&& attributeValueText.length() > 1
&& cursorPosition >= attributeValueEndOffset) {
return;
}
}

switch (attributeContext) {
case EDGE:
switch (attributeName) {
Expand Down Expand Up @@ -600,7 +625,9 @@ private void proposeAttributeValues(String subgrammarName,
} else {
text = text.substring(1, text.length() - 1);
}
offset++;
if (context.getOffset() > offset) {
offset++;
}
}
}

Expand Down Expand Up @@ -681,11 +708,22 @@ private void proposeHtmlLabelAttributeValues(Attribute attribute,
DotColorProposalProvider.globalColorScheme = DotAstHelper
.getColorSchemeAttributeValue(attribute);

if (attribute.getValue() != null
&& attribute.getValue().getType() == Type.HTML_STRING) {
proposeAttributeValues(
DotActivator.ORG_ECLIPSE_GEF_DOT_INTERNAL_LANGUAGE_DOTHTMLLABEL,
context, acceptor);
ID attributeValue = attribute.getValue();
if (attributeValue != null) {
int cursorPosition = context.getOffset();
int attributeValueStartOffset = NodeModelUtils
.findNodesForFeature(attribute, ATTRIBUTE__VALUE).get(0)
.getOffset();
/*
* do not propose the html-label values if the attribute value is
* set, but the cursor is located before it
*/
if (cursorPosition > attributeValueStartOffset
&& attributeValue.getType() == Type.HTML_STRING) {
proposeAttributeValues(
DotActivator.ORG_ECLIPSE_GEF_DOT_INTERNAL_LANGUAGE_DOTHTMLLABEL,
context, acceptor);
}
}

// reset the state of the DotColorProposalProvider
Expand Down

0 comments on commit 45890d5

Please sign in to comment.