Eclipse插件:每次输入内容时折叠部件展开

时间:2016-06-14 14:35:25

标签: java eclipse eclipse-plugin eclipse-rcp

我有什么:我在编辑器插件中添加了一个协调策略,以确定折叠位置以启用代码折叠。它运行得很好,但如果我执行我的插件,折叠一些代码并输入一些东西,所有折叠的代码部分展开自己。我观察到,调和(IRegion分区)是唯一被调用的方法,分区的偏移量始终为0,长度始终是整个文档的长度。所以总是只有一个分区。如何改变?

协调策略的代码:

public class TFReconcilingStrategy implements IReconcilingStrategy {

    IDocument doc = null;
    TextEditorImpl editor = null;
    ISourceViewer sourceViewer = null;
    ModelState state = null;

    public void reconcile(IRegion partition) {
        updateEditor(partition.getOffset(),partition.getLength());
    }

    private void updateEditor(int offset, int length){
        if (editor == null)
            return;
        createFoldingPositions(offset, length);
        editor.redrawViewer();
    }


    //Determines where foldable positions are
    private void createFoldingPositions(int offset, int length) {
        final ArrayList<Position> positions = new ArrayList<Position>();
        String content = editor.getDocument().get();
        int line = 1, col = 1;
        for(int i = 0; i < content.length(); i++){
            if (content.charAt(i) == '\n'){
                line++;
                col = 1;
             } else {
                col++;
             } //Curly Bracket Handling
            if(content.charAt(i) == '{'){
                int startOffset = 0;
                try {
                    startOffset = doc.getLineOffset(line-1) + col -1;
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
                int endOffset = getMatchingBracketPosition(line, col, i, content, '{', '}', 'z');
                if (offset >= 0 && length > 0){
                    if (!(startOffset >= offset && endOffset <= (offset + length)))
                        continue;
                    }
                if(endOffset != -1 && endOffset > 0){
                    Position pos = new Position(startOffset, (endOffset-startOffset));
                    positions.add(pos);
                }

            }//Edge Bracket Handling
            if(content.charAt(i) == '[' && content.charAt(i+1) == '['){
                int startOffset = 0;
                try {
                    startOffset = doc.getLineOffset(line-1) + col -1;
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
                int endOffset = getMatchingBracketPosition(line, col, i, content, '[', ']', 'z');
                if (offset >= 0 && length > 0){
                    if (!(startOffset >= offset && endOffset <= (offset + length)))
                        continue;
                    }
                if(endOffset != -1&& endOffset >0){ //if endoffset = -1 then the matching bracket is located in the same line
                    Position pos = new Position(startOffset, (endOffset-startOffset));
                    positions.add(pos);
                }
            }//Comment Handling
            if(content.charAt(i) == '/' && content.charAt(i+1) == '*'){
                int startOffset = 0;
                try {
                    startOffset = doc.getLineOffset(line-1) + col -1;
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
                int endOffset = getMatchingBracketPosition(line, col, i, content, '/', '*', '/');
                if (offset >= 0 && length > 0){
                    if (!(startOffset >= offset && endOffset <= (offset + length)))
                        continue;
                    }
                if(endOffset != -1 && endOffset >0){ //if endoffset = -1 then the matching bracket is located in the same line
                    Position pos = new Position(startOffset, (endOffset-startOffset));
                    positions.add(pos);
                }
            }
        }
        if (positions.size() > 0)
        {
            Display.getDefault().asyncExec(new Runnable() {

                public void run() {
                    editor.updateFoldingStructure(positions);
                    editor.redrawViewer();
                }

            });
        }       
    }

    public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
        updateEditor(dirtyRegion.getOffset(), dirtyRegion.getLength());
    }

    public void setDocument(IDocument document) {
        doc = document;
        updateEditor(-1,-1);
    }

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

    public void setSourceViewer (ISourceViewer viewer){
        this.sourceViewer = viewer;
    }

    // param add is used, if the end of the foldable area is indicated by two chars
    private int getMatchingBracketPosition(int line, int column, int position, String content, char openBracketType, char closedBracketType, char add){
        int endOffset = 0;
        int j = position;
        boolean found = false;
        int startLine = line;
        int ignoredBrackets = 0;
        while (j < content.length() && !found){
            if (content.charAt(j) == '\n'){
                line++;
                column = 1;
            } else {
                column++;
            }
            if(content.charAt(j) == openBracketType){
                ignoredBrackets++;
            }
            if (add == 'z'){
                if (content.charAt(j) == closedBracketType){
                    ignoredBrackets--;
                    if(ignoredBrackets == 0){
                        found = true;
                        if(startLine != line){
                            try {
                                endOffset = doc.getLineOffset(line-1) + column -1;
                            } catch (BadLocationException e) {
                                e.printStackTrace();
                            }
                        }
                        else endOffset = -1;
                    }
                }
            } else {
                if (content.charAt(j) == closedBracketType && content.charAt(j+1) == add){
                    ignoredBrackets--;
                    if(ignoredBrackets == 0){
                        found = true;
                        if(startLine != line){
                            try {
                                endOffset = doc.getLineOffset(line-1) + column -1;
                            } catch (BadLocationException e) {
                                e.printStackTrace();
                            }
                        }
                        else endOffset = -1;
                    }
                }
            }

            j++;
        }
        return endOffset;
    }
}

我的TextEditor实现中的代码:

public void redrawViewer(){
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                //refresh view
                if ( getSourceViewer() != null)
                     getSourceViewer().getTextWidget().redraw();
            }
        });

  }

和:

public void updateFoldingStructure(ArrayList positions){
        Annotation[] annotations = new Annotation[positions.size()];

        //this will hold the new annotations along
        //with their corresponding positions
        HashMap newAnnotations = new HashMap();
        for(int i = 0; i < positions.size(); i++){
            ProjectionAnnotation annotation = new ProjectionAnnotation();
            newAnnotations.put(annotation,positions.get(i));
            annotations[i]=annotation;
        }
        annotationModel.modifyAnnotations(oldAnnotations,newAnnotations,null);  
        oldAnnotations=annotations;
  }

我想要的东西:我想,即使我打字,折叠的部分也会保持折叠状态。希望有人可以帮助我!

0 个答案:

没有答案