Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Code folding, where is my problem?(Problem of the patrition code folding.)
Code folding, where is my problem? [message #1769703] Wed, 02 August 2017 19:03
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
Hi, all

My goal is creation of code folding functionality on our eclipse plug-in project. We develop editor of the complicated text files what contains from the several parts.

To implements it I created the demo project https://github.com/vladimirkozhaev/simpleeditor.

If I allow only XML content in my file everything is fine and folding works.
Please, see my code bellow

package org.newlanguageservice.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.reconciler.DirtyRegion;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
import org.eclipse.swt.widgets.Display;

import com.idc.omega.xml.XMLNodeInfo;
import com.idc.omega.xml.XMLParserVisitor;

public class SimpleReconciclingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {
	private MinimalEditor editor;

	private IDocument fDocument;

	/** holds the calculated positions */
	protected final List<Position> fPositions = new ArrayList<Position>();

	@Override
	public void setProgressMonitor(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	public void initialReconcile() {

		calculatePositions();

	}

	private void calculatePositions() {
		fPositions.clear();
		String string = fDocument.get();
		try {
			XMLNodeInfo makeNodesInfo = XMLParserVisitor.makeNodesInfo(string);
			System.out.println(makeNodesInfo);
			fillPositions(fPositions, makeNodesInfo);

			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					editor.updateFoldingStructure(fPositions);
				}

			});
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void fillPositions(List<Position> fPositions, XMLNodeInfo makeNodesInfo) {
		if (makeNodesInfo == null) {
			return;
		}
		int offset = makeNodesInfo.getOffset();
		int lenght = makeNodesInfo.getLenght();
		Position position = new Position(offset, lenght);
		fPositions.add(position);
		makeNodesInfo.getChildren().forEach(child -> fillPositions(fPositions, child));

	}

	@Override
	public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
		initialReconcile();

	}

	@Override
	public void reconcile(IRegion partition) {
		initialReconcile();

	}

	public void setEditor(MinimalEditor editor) {
		this.editor = editor;
	}

	public void setDocument(IDocument document) {
		this.fDocument = document;
	}

}



But when try to folde only part between tags XML>> and <<XML I have problem.

Please, see code with changes bellow
package org.newlanguageservice.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.reconciler.DirtyRegion;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
import org.eclipse.swt.widgets.Display;

import com.idc.omega.xml.XMLNodeInfo;
import com.idc.omega.xml.XMLParserVisitor;

public class SimpleReconciclingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {
	private MinimalEditor editor;

	private IDocument fDocument;

	/** holds the calculated positions */
	protected final List<Position> fPositions = new ArrayList<Position>();

	@Override
	public void setProgressMonitor(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	public void initialReconcile() {

		calculatePositions();

	}

	private void calculatePositions() {
		fPositions.clear();
		String string = fDocument.get();
		try {

			[i]while (string.length() > 0) {
				int startIndex = string.indexOf("XML>>");
				int endIndex = string.indexOf("<<XML");

				if (!(startIndex >= 0 && endIndex > 0))
					break;
				String substring = string.substring(startIndex + "XML>>".length(), endIndex);
				XMLNodeInfo makeNodesInfo = XMLParserVisitor.makeNodesInfo(substring);
				fillPositions(fPositions, makeNodesInfo);
				string = string.substring(endIndex + "<<XML".length());

			}[/i]

			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					editor.updateFoldingStructure(fPositions);
				}

			});
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void fillPositions(List<Position> fPositions, XMLNodeInfo makeNodesInfo) {
		if (makeNodesInfo == null) {
			return;
		}
		int offset = makeNodesInfo.getOffset();
		int lenght = makeNodesInfo.getLenght();
		Position position = new Position(offset, lenght);
		fPositions.add(position);
		makeNodesInfo.getChildren().forEach(child -> fillPositions(fPositions, child));

	}

	@Override
	public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
		initialReconcile();

	}

	@Override
	public void reconcile(IRegion partition) {
		initialReconcile();

	}

	public void setEditor(MinimalEditor editor) {
		this.editor = editor;
	}

	public void setDocument(IDocument document) {
		this.fDocument = document;
	}

}



After mentoned changes code is not working.

Where I'm wrong?
Regards,
Vladimir
Previous Topic:Tutorial to implement eclipse outline content provider
Next Topic:[Feature Proposal / Discussion] Enhance Analysis of compile-time classpath
Goto Forum:
  


Current Time: Fri Apr 26 19:57:36 GMT 2024

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

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

Back to the top