Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Dynamic Languages Toolkit (DLTK) » Implement code folding(How to enable code folding in a DLTK editor?)
Implement code folding [message #506417] Thu, 07 January 2010 13:28 Go to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
Hi,

I have been searching the forum for a while but I wasn't able to find a satifying answer. How to enable code folding in a DLTK based editor. I've got both DLTK parser and editor, I just would like to fold function in the editor. Does anyone know where I should look?

Regards

[Updated on: Thu, 07 January 2010 13:32]

Report message to a moderator

Re: Implement code folding [message #506519 is a reply to message #506417] Thu, 07 January 2010 20:05 Go to previous messageGo to next message
Alex Panchenko is currently offline Alex PanchenkoFriend
Messages: 342
Registered: July 2009
Senior Member
Hi Kevin,

In your editor:

private IFoldingStructureProvider foldingStructureProvider = null;

@Override
protected IFoldingStructureProvider getFoldingStructureProvider() {
	if (foldingStructureProvider == null) {
		foldingStructureProvider = new ExamplePythonFoldingStructureProvider();
	}
	return foldingStructureProvider;
}


Minimal implementation of the folding provider:
public class ExamplePythonFoldingStructureProvider extends AbstractASTFoldingStructureProvider {

	public String getCommentPartition() {
		return IExamplePythonPartitions.PYTHON_COMMENT;
	}

	protected ILog getLog() {
		return ExamplePythonUI.getDefault().getLog();
	}

	protected String getPartition() {
		return IExamplePythonPartitions.PYTHON_PARTITIONING;
	}

	protected IPartitionTokenScanner getPartitionScanner() {
		return ExamplePythonUI.getDefault().getTextTools()
				.getPartitionScanner();
	}

	protected String[] getPartitionTypes() {
		return IExamplePythonPartitions.PYTHON_PARITION_TYPES;
	}

	protected String getNatureId() {
		return ExamplePythonNature.PYTHON_NATURE;
	}
}


I've update example editor in CVS, but it seems ViewCVS is updated with a delay.

Regards,
Alex
Re: Implement code folding [message #506608 is a reply to message #506519] Fri, 08 January 2010 10:23 Go to previous messageGo to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
Thanks for your quick answer.
I've implemented the minimal folding, just to have a quick look. Functions don't fold yet. I think that's pretty normal as functions aren't partitionned yet.
Anyway, comments are partitionned and they don't fold either. Am I forgetting something? The comments and strings are in color, that mean that ther are parsed and the editor is aware of their existence.

Here my partitions:
public interface ILuaPartitions {
	public static final String LUA_PARTITIONING = LuaConstants.LUA_PARTITIONING;

	public static final String LUA_COMMENT = "__lua_comment"; //$NON-NLS-1$
	public static final String LUA_MULTI_LINE_COMMENT = "__lua_multi_line_comment"; //$NON-NLS-1$
	public static final String LUA_STRING = "__lua_string"; //$NON-NLS-1$
	public static final String LUA_SINGLE_QUOTE_STRING = "__lua_single_quote_string"; //$NON-NLS-1$

	public final static String[] LUA_PARTITION_TYPES = new String[] {
			IDocument.DEFAULT_CONTENT_TYPE, ILuaPartitions.LUA_COMMENT,
			ILuaPartitions.LUA_STRING, ILuaPartitions.LUA_SINGLE_QUOTE_STRING,
			ILuaPartitions.LUA_MULTI_LINE_COMMENT };
}

And the associated rules:
public class LuaPartitionScanner extends RuleBasedPartitionScanner {

	public LuaPartitionScanner() {
		super();
		List<PatternRule> rules = new ArrayList<PatternRule>();

		/*
		 * Deal with single and double quote multi lines strings
		 */
		IToken string = new Token(ILuaPartitions.LUA_STRING);
		IToken singleQuoteString = new Token(
				ILuaPartitions.LUA_SINGLE_QUOTE_STRING);
		rules
				.add(new MultiLineRule(
						"\'", "\'", singleQuoteString, '\\', false)); //$NON-NLS-1$ //$NON-NLS-2$
		rules.add(new MultiLineRule("\"", "\"", string, '\\', false)); //$NON-NLS-1$ //$NON-NLS-2$

		/*
		 * Deal with comments
		 */

		// Multi-line
		IToken multiLineComment = new Token(
				ILuaPartitions.LUA_MULTI_LINE_COMMENT);
		rules.add(new MultiLineRule("--[[", "]]", multiLineComment));//$NON-NLS-1$

		// Single line
		IToken comment = new Token(ILuaPartitions.LUA_COMMENT);
		rules.add(new EndOfLineRule("--", comment)); //$NON-NLS-1$

		// Apply rules
		IPredicateRule[] result = new IPredicateRule[rules.size()];
		rules.toArray(result);
		setPredicateRules(result);
	}

}
Re: Implement code folding [message #506744 is a reply to message #506608] Sat, 09 January 2010 11:35 Go to previous messageGo to next message
Alex Panchenko is currently offline Alex PanchenkoFriend
Messages: 342
Registered: July 2009
Senior Member
Hi Kevin,

Most probably you should set default value for PreferenceConstants.EDITOR_FOLDING_ENABLED preference to true.

For example of the folding preference page you can look at RubyFoldingPreferencePage.

Comments are folded based on partitions and functions should be folded based on AST (if nodes extend org.eclipse.dltk.ast.declarations.MethodDeclaration).

Regards,
Alex
Re: Implement code folding [message #507391 is a reply to message #506744] Wed, 13 January 2010 10:44 Go to previous messageGo to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
Hi,

I don't have preference page yet, but reading the code I've see that this constant has enabled as default value.
/**
	 * A named preference that controls whether folding is enabled in the Script
	 * editor.
	 * <p>
	 * Value is of type <code>Boolean</code>.
	 * </p>
	 */
	public static final String EDITOR_FOLDING_ENABLED = "editor_folding_enabled"; //$NON-NLS-1$

Is it relevant or is it a value that keeps the folding silent.

Thanks
Re: Implement code folding [message #507430 is a reply to message #507391] Wed, 13 January 2010 13:37 Go to previous messageGo to next message
Alex Panchenko is currently offline Alex PanchenkoFriend
Messages: 342
Registered: July 2009
Senior Member
Hi Kevin,

I am not sure what makes you think it's enabled by default. If you want it to, then you should set it

<extension point="org.eclipse.core.runtime.preferences">
   <initializer class="MyPreferenceInitializer"/>
</extension>


public class MyPreferenceInitializer extends AbstractPreferenceInitializer {

	@Override
	public void initializeDefaultPreferences() {
		IPreferenceStore store = MyUI.getDefault().getPreferenceStore();
....
		store.setDefault(PreferenceConstants.EDITOR_FOLDING_ENABLED, true);
....
	}
}


It's used in ScriptEditor as following:

private boolean isFoldingEnabled() {
  return getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
}


Regards,
Alex

[Updated on: Thu, 14 January 2010 07:13]

Report message to a moderator

Re: Implement code folding [message #507682 is a reply to message #506744] Thu, 14 January 2010 11:22 Go to previous messageGo to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
I now working great, comment does not fold but I'm on it.
Re: Implement code folding [message #510808 is a reply to message #507682] Thu, 28 January 2010 17:17 Go to previous messageGo to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
Hi,

Even if functions can be folded in my editor, comments don't. Why? Is it because functions are declared and comments are not. Is there any further rules of mapping in addtion of partitions?

Regards
Re: Implement code folding [message #515271 is a reply to message #510808] Thu, 18 February 2010 10:29 Go to previous message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
I've manages to fold comments. In your class inherited from AbstractPreferenceInitializer
public class YourPreferenceInitializer extends AbstractPreferenceInitializer {

	@Override
	public void initializeDefaultPreferences() {
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		// ...		
		store.setDefault( PreferenceConstants.EDITOR_DOCS_FOLDING_ENABLED, true);

	}

}
Previous Topic:How to highlight reconciliation error in the texteditor
Next Topic:[BUILD] I-I201002221005-201002221005
Goto Forum:
  


Current Time: Fri Apr 19 11:00:55 GMT 2024

Powered by FUDForum. Page generated in 0.02342 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top