Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be7369e4aa7a4b3081fa6084abc4166789f82382 (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
/*******************************************************************************
 * Copyright (c) 2006, 2017 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Kyu Lee <klee@redhat.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.changelog.core.editors;

import java.util.Map;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.formatter.ContentFormatter;
import org.eclipse.jface.text.formatter.IContentFormatter;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.linuxtools.changelog.core.IEditorChangeLogContrib;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;


/**
 * GNU format ChangeLog editor configuration.
 *
 * @author klee (Kyu Lee)
 */
public class GNUEditorConfiguration extends TextSourceViewerConfiguration implements
        IEditorChangeLogContrib {

    public static final String CHANGELOG_PARTITIONING= "gnu_changelog_partitioning";  //$NON-NLS-1$

    private GNUElementScanner scanner;

    private ColorManager colorManager;

    private TextEditor parentEditor;

    /**
     * Prepares configuration.
     */
    public GNUEditorConfiguration() {
        this.colorManager = new ColorManager();

    }

    /**
     * Sets TextEditor that this configuration is going to be applied.
     */
    @Override
    public void setTextEditor(TextEditor editor) {
        parentEditor = editor;
    }

    /**
     * Get configured content types.
     *
     * @return array of configured content types.
     */
    @Override
    public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
        return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
                  GNUPartitionScanner.CHANGELOG_EMAIL,
                  GNUPartitionScanner.CHANGELOG_SRC_ENTRY};
    }

    private GNUElementScanner getChangeLogFileScanner() {
        if (scanner == null) {
            scanner = new GNUElementScanner(colorManager);
            scanner.setDefaultReturnToken(new Token(new TextAttribute(
                    colorManager.getColor(IChangeLogColorConstants.TEXT))));
        }
        return scanner;
    }

    /**
     * Detects hyperlinks in GNU formatted changelogs.
     *
     * @return link detector for GNU format.
     */
    @Override
    public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
        if (sourceViewer == null)
            return null;

        return getRegisteredHyperlinkDetectors(sourceViewer);
    }

    @Override
    public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
        return CHANGELOG_PARTITIONING;
    }

    /**
     * Set content formatter. For ChangeLog, it just wraps lines.
     */
    @Override
    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {

        ContentFormatter cf = new ContentFormatter();

        // no partitions
        cf.enablePartitionAwareFormatting(false);

        ChangeLogFormattingStrategy cfs = new ChangeLogFormattingStrategy();

        cf.setFormattingStrategy(cfs, IDocument.DEFAULT_CONTENT_TYPE);


        return cf;
    }


    /**
     * Highlights GNU format changelog syntaxes.
     *
     * @return reconciler for GNU format changelog.
     */
    @Override
    public IPresentationReconciler getPresentationReconciler(
            ISourceViewer sourceViewer) {
        PresentationReconciler reconciler = new PresentationReconciler();

        DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getChangeLogFileScanner());
        reconciler.setDamager(dr, GNUPartitionScanner.CHANGELOG_EMAIL);
        reconciler.setRepairer(dr, GNUPartitionScanner.CHANGELOG_EMAIL);

        dr= new GNUFileEntryDamagerRepairer(getChangeLogFileScanner());
        reconciler.setDamager(dr, GNUPartitionScanner.CHANGELOG_SRC_ENTRY);
        reconciler.setRepairer(dr, GNUPartitionScanner.CHANGELOG_SRC_ENTRY);

        dr= new MultilineRuleDamagerRepairer(getChangeLogFileScanner());
        reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
        reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

        return reconciler;
    }

    /**
     * Perform documentation setup to set up partitioning.
     *
     * @param document to set up partitioning on.
     */
    @Override
    public void setup(IDocument document) {
        FastPartitioner partitioner =
            new FastPartitioner(
                new GNUPartitionScanner(),
                GNUPartitionScanner.CHANGELOG_PARTITION_TYPES);
        partitioner.connect(document);
        if (document instanceof IDocumentExtension3) {
            IDocumentExtension3 extension3= (IDocumentExtension3) document;
            extension3.setDocumentPartitioner(CHANGELOG_PARTITIONING, partitioner);
        } else {
            document.setDocumentPartitioner(partitioner);
        }
    }

    @Override
    protected Map<String, IAdaptable> getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
        Map<String, IAdaptable> targets = super.getHyperlinkDetectorTargets(sourceViewer);
        targets.put("org.eclipse.changelog.editor.target", parentEditor); //$NON-NLS-1$
        targets.put("org.eclipse.ui.DefaultTextEditor", parentEditor); //$NON-NLS-1$
        return targets;
    }}

Back to the top