Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cb922faf47c3bf7492dd0c9c1806d2caddc7f14 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text.projection;


import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextStore;
import org.eclipse.jface.text.Region;


/**
 * A text store representing the projection defined by the given document
 * information mapping.
 *
 * @since 3.0
 */
class ProjectionTextStore implements ITextStore {

	/**
	 * Implementation of {@link IRegion} that can be reused
	 * by setting the offset and the length.
	 */
	private static class ReusableRegion implements IRegion {

		private int fOffset;
		private int fLength;

		@Override
		public int getLength() {
			return fLength;
		}

		@Override
		public int getOffset() {
			return fOffset;
		}

		/**
		 * Updates this region.
		 *
		 * @param offset the new offset
		 * @param length the new length
		 */
		public void update(int offset, int length) {
			fOffset= offset;
			fLength= length;
		}
	}

	/** The master document */
	private IDocument fMasterDocument;
	/** The document information mapping */
	private IMinimalMapping fMapping;
	/** Internal region used for querying the mapping. */
	private ReusableRegion fReusableRegion= new ReusableRegion();


	/**
	 * Creates a new projection text store for the given master document and
	 * the given document information mapping.
	 *
	 * @param masterDocument the master document
	 * @param mapping the document information mapping
	 */
	public ProjectionTextStore(IDocument masterDocument, IMinimalMapping mapping) {
		fMasterDocument= masterDocument;
		fMapping= mapping;
	}

	private void internalError() {
		throw new IllegalStateException();
	}

	@Override
	public void set(String contents) {

		IRegion masterRegion= fMapping.getCoverage();
		if (masterRegion == null)
			internalError();

		try {
			fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), contents);
		} catch (BadLocationException e) {
			internalError();
		}
	}

	@Override
	public void replace(int offset, int length, String text) {
		fReusableRegion.update(offset, length);
		try {
			IRegion masterRegion= fMapping.toOriginRegion(fReusableRegion);
			fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), text);
		} catch (BadLocationException e) {
			internalError();
		}
	}

	@Override
	public int getLength() {
		return fMapping.getImageLength();
	}

	@Override
	public char get(int offset) {
		try {
			int originOffset= fMapping.toOriginOffset(offset);
			return fMasterDocument.getChar(originOffset);
		} catch (BadLocationException e) {
			internalError();
		}

		// unreachable
		return (char) 0;
	}

	@Override
	public String get(int offset, int length) {
		try {
			IRegion[] fragments= fMapping.toExactOriginRegions(new Region(offset, length));
			StringBuilder buffer= new StringBuilder();
			for (int i= 0; i < fragments.length; i++) {
				IRegion fragment= fragments[i];
				buffer.append(fMasterDocument.get(fragment.getOffset(), fragment.getLength()));
			}
			return buffer.toString();
		} catch (BadLocationException e) {
			internalError();
		}

		// unreachable
		return null;
	}
}

Back to the top