Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6a4f6efc9eff4caff6eccbb67de4e75ead1661e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
 * Copyright (c) 2008, 2018 Phil Muldoon 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 https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Phil Muldoon <pkmuldoon@picobot.org> - initial API.
 *******************************************************************************/

package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;

public class STPEditor extends TextEditor {

    private Annotation[] stpOldAnnotations;
    private ProjectionAnnotationModel stpAnnotationModel;

    public static final String ID="org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor"; //$NON-NLS-1$

    public STPEditor() {
        super();
        setKeyBindingScopes(new String[] { "org.eclipse.linuxtools.systemtap.ui.ide.context" }); //$NON-NLS-1$
        configureInsertMode(SMART_INSERT, false);
        setSourceViewerConfiguration(new STPConfiguration(this));
    }

    @Override
    protected void doSetInput(IEditorInput input) throws CoreException {
        if(input instanceof FileStoreEditorInput) {
            input= new PathEditorInput(new Path(((FileStoreEditorInput) input).getURI().getPath()));
        }
        super.doSetInput(input);
    }

    @Override
    public void createPartControl(Composite parent)
    {
        super.createPartControl(parent);
       ProjectionViewer viewer =(ProjectionViewer)getSourceViewer();
       ProjectionSupport stpProjectionSupport = new ProjectionSupport(viewer,getAnnotationAccess(),getSharedColors());
       stpProjectionSupport.install();
       viewer.doOperation(ProjectionViewer.TOGGLE);
       stpAnnotationModel = viewer.getProjectionAnnotationModel();
    }

    @Override
    protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {

        ISourceViewer viewer = new ProjectionViewer(parent, ruler,
                getOverviewRuler(), isOverviewRulerVisible(), styles);
        getSourceViewerDecorationSupport(viewer);
        return viewer;
    }


	public void updateFoldingStructure(ArrayList<Position> updatedPositions) {
		ProjectionAnnotation annotation;
		Annotation[] updatedAnnotations = new Annotation[updatedPositions.size()];
		HashMap<ProjectionAnnotation, Position> newAnnotations = new HashMap<>();
		for (int i = 0; i < updatedPositions.size(); i++) {
			annotation = new ProjectionAnnotation();
			newAnnotations.put(annotation, updatedPositions.get(i));
			updatedAnnotations[i] = annotation;
		}
		stpAnnotationModel.modifyAnnotations(stpOldAnnotations, newAnnotations, null);
		stpOldAnnotations = updatedAnnotations;
	}

	public ISourceViewer getMySourceViewer() {
		return this.getSourceViewer();
	}

	 /**
     * Inserts text into the IDocument.
     * @param text string to insert
     */
    public void insertText(String text) {
        IDocument doc = getSourceViewer().getDocument();
        String s = doc.get();
        int offset = s.length();
        s += text;
        doc.set(s);
        this.setHighlightRange(offset, 0, true);
    }

    /**
     * Inserts text at the current location.
     * @param text string to insert
     */
    public void insertTextAtCurrent(String text) {
        ISelection selection = this.getSelectionProvider().getSelection();
        IDocument doc = getSourceViewer().getDocument();

        if (selection instanceof ITextSelection) {
            ITextSelection s = (ITextSelection) selection;
            StringBuffer sb = new StringBuffer(doc.get().substring(0, s.getOffset()));
            sb.append(text.trim());
            sb.append(doc.get().substring(s.getOffset() + s.getLength(), doc.get().length()));
            doc.set(sb.toString());
            this.setHighlightRange(s.getOffset() + text.trim().length(), 0, true);
        }
    }

    /**
     * Jumps to the location in the IDocument.
     * @param line The line you wish to jump to.
     * @param character The character you wish to jump to.
     */
    public void jumpToLocation(int line, int character) {
        IDocument doc = getSourceViewer().getDocument();

        try {
            int offset = doc.getLineOffset(line-1) + character;
            this.getSelectionProvider().setSelection(new TextSelection(doc, offset, 0));
        } catch (BadLocationException boe) {
            // Pass
        }
    }

    /**
     * Selects a line in the IDocument.
     * @param line the line you wish to select
     */
    public void selectLine(int line) {
        IDocument doc = getSourceViewer().getDocument();

        try {
            this.getSelectionProvider().setSelection(new TextSelection(doc, doc.getLineOffset(line-1), doc.getLineLength(line-1)-1));
        } catch (BadLocationException boe) {
            // Pass
        }
    }

    /**
     * Performs a SaveAs on the IDocument.
     */
    @Override
    public void doSaveAs() {
        File file = queryFile();
        if (file == null) {
            return;
        }

        IEditorInput inputFile = createEditorInput(file);

        IDocument doc = getSourceViewer().getDocument();
        String s = doc.get();

        try (FileOutputStream fos = new FileOutputStream(file);
                PrintStream ps = new PrintStream(fos)){
            ps.print(s);
            ps.close();
        } catch (IOException fnfe) {
            // Pass
        }

        setInput(inputFile);
        setPartName(inputFile.getName());
    }

    /**
     * Sets up an editor input based on the specified file.
     * @param file the location of the file you wish to set.
     * @return input object created.
     */
    private static IEditorInput createEditorInput(File file) {
        IPath location= new Path(file.getAbsolutePath());
        return new PathEditorInput(location);
    }

    private static File queryFile() {
        FileDialog dialog= new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
        dialog.setText(Messages.NewFileHandler_NewFile);
        String path= dialog.open();
        if (path != null && !path.isEmpty()) {
            return new File(path);
        }
        return null;
    }

    @Override
	protected void editorContextMenuAboutToShow(IMenuManager menu) {
		super.editorContextMenuAboutToShow(menu);
		addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_RIGHT);
		addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_LEFT);

	}

}

Back to the top