Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 351644 - [Auto Edit] Issue with comment terminals with common prefix
Summary: [Auto Edit] Issue with comment terminals with common prefix
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard: v2.4.3
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-09 17:05 EDT by Dénes Harmath CLA
Modified: 2017-10-31 10:58 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dénes Harmath CLA 2011-07-09 17:05:43 EDT
Build Identifier: I20110613-1736

My grammar's comment terminal rules look like this:

terminal SL_COMMENT: "%" !("\n" | "\r")* ("\r"? "\n")?;
terminal ML_COMMENT: "%{" -> "%}";

I register a SingleLineTerminalsStrategy and a MultiLineTerminalsStrategy for %{ %}. There are two problems:
- When typing %{ into an empty document, it is not autocompleted. (Condition "command.text.length() - command.length + documentContent.length() >= getLeftTerminal().length()" in SingleLineTerminalsStrategy returns false.)
- Otherwise, when having typed %{, it is autocompleted, and I press enter, no indentation is applied. I get
%{
<cursor>%}
instead of
%{
    <cursor>
%}

Reproducible: Always
Comment 1 Dénes Harmath CLA 2013-07-01 11:08:15 EDT
With 2.4.2.v201306120542, only one problem remains: when typing %{ into an empty document, it is autocompleted to %{}, not %{%}
Comment 2 Sebastian Zarnekow CLA 2013-07-12 04:32:51 EDT
Preliminary scheduled for 2.4.3 since Anton's currently looking into auto edit anyway.
Comment 3 Anton Kosyakov CLA 2013-07-23 07:57:48 EDT
Pushed to Gerrit: https://git.eclipse.org/r/#/c/14772/

It seems that the issue is with the grammar. Both single line comment and multiline comment start with % character.

So if you type %{ then it will be beginning of single line comment and there is no reason to add %}. Autocompletion of } in that case is the bug because we don't support auto edit for {} in the context of single line comment.

You can try to use two % character in a row for SL_COMMENT to overcome ambiguity:
terminal SL_COMMENT:
	"%%" !("\n" | "\r")* ("\r"? "\n")?; 

But in that case you will run into the problem with auto edit for %{%} in the single line. So if you type %{ you will get %{}. 
The reason is that the auto edit for {} works before auto edit for %{%}. You will have to change the order to fix this problem:

package org.xtext.example.mydsl.ui.editor.autoedit

import org.eclipse.xtext.ui.editor.autoedit.DefaultAutoEditStrategyProvider
import org.eclipse.xtext.ui.editor.autoedit.AbstractEditStrategyProvider.IEditStrategyAcceptor
import org.eclipse.jface.text.IDocument
import org.eclipse.xtext.ui.editor.model.TerminalsTokenTypeToPartitionMapper

class Bug351644AutoEditStrategyProvider extends DefaultAutoEditStrategyProvider {

	override configure(IEditStrategyAcceptor acceptor) {
		configureIndentationEditStrategy(acceptor);
		configureStringLiteral(acceptor);
		configureParenthesis(acceptor);
		configureSquareBrackets(acceptor);
		configureMultilineComments(acceptor); // (1) <-- swop these two lines
		configureCurlyBracesBlock(acceptor); 	// (2)
		configureCompoundBracesBlocks(acceptor);
	}

	override protected configureMultilineComments(extension IEditStrategyAcceptor acceptor) {
		singleLineTerminals.newInstance("%{", "%}").accept(IDocument.DEFAULT_CONTENT_TYPE);
		multiLineTerminals.newInstance("%{", null, "%}").accept(IDocument.DEFAULT_CONTENT_TYPE);
		multiLineTerminals.newInstance("%{", null, "%}").accept(TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION);
	}

}
Comment 4 Anton Kosyakov CLA 2013-07-25 08:26:07 EDT
Merged into repo.
Comment 5 Eclipse Webmaster CLA 2017-10-31 10:47:25 EDT
Requested via bug 522520.

-M.
Comment 6 Eclipse Webmaster CLA 2017-10-31 10:58:32 EDT
Requested via bug 522520.

-M.