blob: 9488df905ae20e3de1fb6d2b8ee4e68f22eebb93 [file] [log] [blame]
nitind93023a12006-09-19 18:24:13 +00001/*******************************************************************************
2 * Copyright (c) 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *
11 *******************************************************************************/
paverydc8b3062005-10-24 20:43:42 +000012package org.eclipse.wst.sse.ui.internal.reconcile;
13
paverydc8b3062005-10-24 20:43:42 +000014import java.io.IOException;
nitindc77f9632008-03-19 21:26:39 +000015import java.io.StringReader;
nitind93023a12006-09-19 18:24:13 +000016import java.util.ArrayList;
17import java.util.List;
paverydc8b3062005-10-24 20:43:42 +000018
19import org.eclipse.core.runtime.Platform;
20import org.eclipse.core.runtime.content.IContentDescription;
21import org.eclipse.core.runtime.content.IContentType;
paverydc8b3062005-10-24 20:43:42 +000022import org.eclipse.jface.text.IDocument;
paverydc8b3062005-10-24 20:43:42 +000023import org.eclipse.jface.text.ITypedRegion;
nitindf3f10872007-09-03 06:36:56 +000024import org.eclipse.jface.text.Region;
paverydc8b3062005-10-24 20:43:42 +000025import org.eclipse.jface.text.reconciler.DirtyRegion;
26import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
paverydc8b3062005-10-24 20:43:42 +000027import org.eclipse.jface.text.source.ISourceViewer;
28import org.eclipse.wst.sse.ui.internal.IReleasable;
nitind93023a12006-09-19 18:24:13 +000029import org.eclipse.wst.sse.ui.internal.Logger;
paverydc8b3062005-10-24 20:43:42 +000030import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorBuilder;
31import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorMetaData;
32import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorStrategy;
nitindb0445972006-02-16 22:19:20 +000033import org.eclipse.wst.sse.ui.internal.spelling.SpellcheckStrategy;
paverydc8b3062005-10-24 20:43:42 +000034
35/**
nitindb0445972006-02-16 22:19:20 +000036 * Adds to DirtyRegionProcessor Job: - IDocumentListener - ValidatorStrategy -
37 * Text viewer(dispose, input changed) listeners. - default, spelling, and
38 * validator strategies - DirtyRegion processing logic.
paverydc8b3062005-10-24 20:43:42 +000039 */
nitindb0445972006-02-16 22:19:20 +000040public class DocumentRegionProcessor extends DirtyRegionProcessor {
paverydc8b3062005-10-24 20:43:42 +000041
nitindc77f9632008-03-19 21:26:39 +000042 private static final boolean DEBUG_VALIDATORS = Boolean.TRUE.toString().equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.sse.ui/debug/reconcilerValidators")); //$NON-NLS-1$
nitind93023a12006-09-19 18:24:13 +000043
paverydc8b3062005-10-24 20:43:42 +000044 /**
nitindb0445972006-02-16 22:19:20 +000045 * A strategy to use the defined default Spelling service.
paverydc8b3062005-10-24 20:43:42 +000046 */
nitindc77f9632008-03-19 21:26:39 +000047 private IReconcilingStrategy fSpellcheckStrategy;
paverydc8b3062005-10-24 20:43:42 +000048
paverydc8b3062005-10-24 20:43:42 +000049 /**
50 * The strategy that runs validators contributed via
51 * <code>org.eclipse.wst.sse.ui.extensions.sourcevalidation</code>
52 * extension point
53 */
54 private ValidatorStrategy fValidatorStrategy;
nitindb0445972006-02-16 22:19:20 +000055
nitinda5804e82006-03-14 00:54:11 +000056 private final String SSE_UI_ID = "org.eclipse.wst.sse.ui"; //$NON-NLS-1$
paverydc8b3062005-10-24 20:43:42 +000057
amywucfaee2d2006-11-21 00:28:32 +000058
nitindb0445972006-02-16 22:19:20 +000059 protected void beginProcessing() {
60 super.beginProcessing();
nitinda5804e82006-03-14 00:54:11 +000061 ValidatorStrategy validatorStrategy = getValidatorStrategy();
62 if (validatorStrategy != null) {
63 validatorStrategy.beginProcessing();
64 }
paverydc8b3062005-10-24 20:43:42 +000065 }
paverydc8b3062005-10-24 20:43:42 +000066
nitindb0445972006-02-16 22:19:20 +000067 protected void endProcessing() {
68 super.endProcessing();
nitinda5804e82006-03-14 00:54:11 +000069 ValidatorStrategy validatorStrategy = getValidatorStrategy();
70 if (validatorStrategy != null) {
71 validatorStrategy.endProcessing();
72 }
nitindb0445972006-02-16 22:19:20 +000073 }
paverydc8b3062005-10-24 20:43:42 +000074
nitindb0445972006-02-16 22:19:20 +000075 protected String getContentType(IDocument doc) {
76 if (doc == null)
77 return null;
paverydc8b3062005-10-24 20:43:42 +000078
nitindb0445972006-02-16 22:19:20 +000079 String contentTypeId = null;
paverydc8b3062005-10-24 20:43:42 +000080
nitindb0445972006-02-16 22:19:20 +000081 IContentType ct = null;
82 try {
nitindc77f9632008-03-19 21:26:39 +000083 IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(new StringReader(doc.get()), null, IContentDescription.ALL);
nitindb0445972006-02-16 22:19:20 +000084 if (desc != null) {
85 ct = desc.getContentType();
86 if (ct != null)
87 contentTypeId = ct.getId();
paverydc8b3062005-10-24 20:43:42 +000088 }
89 }
nitindb0445972006-02-16 22:19:20 +000090 catch (IOException e) {
91 // just bail
paverydc8b3062005-10-24 20:43:42 +000092 }
nitindb0445972006-02-16 22:19:20 +000093 return contentTypeId;
paverydc8b3062005-10-24 20:43:42 +000094 }
paverydc8b3062005-10-24 20:43:42 +000095
nitindc77f9632008-03-19 21:26:39 +000096 protected IReconcilingStrategy getSpellcheckStrategy() {
nitindb0445972006-02-16 22:19:20 +000097 if (fSpellcheckStrategy == null) {
98 String contentTypeId = getContentType(getDocument());
99 if (contentTypeId != null) {
100 if (getTextViewer() instanceof ISourceViewer) {
101 ISourceViewer viewer = (ISourceViewer) getTextViewer();
102 fSpellcheckStrategy = new SpellcheckStrategy(viewer, contentTypeId);
103 fSpellcheckStrategy.setDocument(getDocument());
paverydc8b3062005-10-24 20:43:42 +0000104 }
105 }
paverydc8b3062005-10-24 20:43:42 +0000106 }
nitindb0445972006-02-16 22:19:20 +0000107 return fSpellcheckStrategy;
paverydc8b3062005-10-24 20:43:42 +0000108 }
nitindb0445972006-02-16 22:19:20 +0000109
paverydc8b3062005-10-24 20:43:42 +0000110 /**
nitindb0445972006-02-16 22:19:20 +0000111 * @return Returns the ValidatorStrategy.
paverydc8b3062005-10-24 20:43:42 +0000112 */
nitindb0445972006-02-16 22:19:20 +0000113 protected ValidatorStrategy getValidatorStrategy() {
paverydc8b3062005-10-24 20:43:42 +0000114 if (fValidatorStrategy == null) {
115 ValidatorStrategy validatorStrategy = null;
nitindb0445972006-02-16 22:19:20 +0000116
paverydc8b3062005-10-24 20:43:42 +0000117 if (getTextViewer() instanceof ISourceViewer) {
nitindb0445972006-02-16 22:19:20 +0000118 ISourceViewer viewer = (ISourceViewer) getTextViewer();
paverydc8b3062005-10-24 20:43:42 +0000119 String contentTypeId = null;
nitindb0445972006-02-16 22:19:20 +0000120
paverydc8b3062005-10-24 20:43:42 +0000121 IDocument doc = viewer.getDocument();
122 contentTypeId = getContentType(doc);
nitindb0445972006-02-16 22:19:20 +0000123
paverydc8b3062005-10-24 20:43:42 +0000124 if (contentTypeId != null) {
paverydc8b3062005-10-24 20:43:42 +0000125 validatorStrategy = new ValidatorStrategy(viewer, contentTypeId);
126 ValidatorBuilder vBuilder = new ValidatorBuilder();
nitinda5804e82006-03-14 00:54:11 +0000127 ValidatorMetaData[] vmds = vBuilder.getValidatorMetaData(SSE_UI_ID);
nitind93023a12006-09-19 18:24:13 +0000128 List enabledValidators = new ArrayList(1);
129 /* if any "must" handle this content type, just add them */
130 boolean foundSpecificContentTypeValidators = false;
paverydc8b3062005-10-24 20:43:42 +0000131 for (int i = 0; i < vmds.length; i++) {
nitind93023a12006-09-19 18:24:13 +0000132 if (vmds[i].mustHandleContentType(contentTypeId)) {
133 if (DEBUG_VALIDATORS)
134 Logger.log(Logger.INFO, contentTypeId + " using specific validator " + vmds[i].getValidatorId()); //$NON-NLS-1$
135 foundSpecificContentTypeValidators = true;
136 enabledValidators.add(vmds[i]);
137 }
138 }
139 if (!foundSpecificContentTypeValidators) {
140 for (int i = 0; i < vmds.length; i++) {
141 if (vmds[i].canHandleContentType(contentTypeId)) {
142 if (DEBUG_VALIDATORS)
143 Logger.log(Logger.INFO, contentTypeId + " using inherited(?) validator " + vmds[i].getValidatorId()); //$NON-NLS-1$
144 enabledValidators.add(vmds[i]);
145 }
146 }
147 }
148 for (int i = 0; i < enabledValidators.size(); i++) {
149 validatorStrategy.addValidatorMetaData((ValidatorMetaData) enabledValidators.get(i));
paverydc8b3062005-10-24 20:43:42 +0000150 }
151 }
152 }
153 fValidatorStrategy = validatorStrategy;
154 }
155 return fValidatorStrategy;
156 }
157
paverydc8b3062005-10-24 20:43:42 +0000158 /**
nitindb0445972006-02-16 22:19:20 +0000159 * @param dirtyRegion
paverydc8b3062005-10-24 20:43:42 +0000160 */
nitindb0445972006-02-16 22:19:20 +0000161 protected void process(DirtyRegion dirtyRegion) {
amywucfaee2d2006-11-21 00:28:32 +0000162 if (!isInstalled() || isInRewriteSession())
nitindb0445972006-02-16 22:19:20 +0000163 return;
164
165 super.process(dirtyRegion);
166
167 // Also call the validation and spell-check strategies
168 ITypedRegion[] partitions = computePartitioning(dirtyRegion);
169
170 DirtyRegion dirty = null;
171 for (int i = 0; i < partitions.length; i++) {
nitindb0445972006-02-16 22:19:20 +0000172 // [source]validator (extension) for this partition
david_williams5ce6d6c2006-05-28 23:58:12 +0000173 if (getValidatorStrategy() != null) {
174 dirty = createDirtyRegion(partitions[i], DirtyRegion.INSERT);
nitindb0445972006-02-16 22:19:20 +0000175 getValidatorStrategy().reconcile(partitions[i], dirty);
david_williams5ce6d6c2006-05-28 23:58:12 +0000176 }
pavery1f1588d2006-02-15 20:44:54 +0000177 }
nitind93023a12006-09-19 18:24:13 +0000178
nitindb0445972006-02-16 22:19:20 +0000179 // single spell-check for everything
david_williams5ce6d6c2006-05-28 23:58:12 +0000180 if (getSpellcheckStrategy() != null) {
nitindb0445972006-02-16 22:19:20 +0000181 getSpellcheckStrategy().reconcile(dirtyRegion, dirtyRegion);
david_williams5ce6d6c2006-05-28 23:58:12 +0000182 }
paverydc8b3062005-10-24 20:43:42 +0000183 }
paverydc8b3062005-10-24 20:43:42 +0000184
nitindb0445972006-02-16 22:19:20 +0000185 public void setDocument(IDocument doc) {
186 super.setDocument(doc);
187 IReconcilingStrategy validatorStrategy = getValidatorStrategy();
188 if (validatorStrategy != null) {
189 validatorStrategy.setDocument(doc);
paverydc8b3062005-10-24 20:43:42 +0000190 }
nitind0f059552006-05-01 17:36:50 +0000191 if (fSpellcheckStrategy != null) {
192 fSpellcheckStrategy.setDocument(doc);
193 }
paverydc8b3062005-10-24 20:43:42 +0000194 }
nitindc77f9632008-03-19 21:26:39 +0000195
nitindf3f10872007-09-03 06:36:56 +0000196 protected void setEntireDocumentDirty(IDocument document) {
197 super.setEntireDocumentDirty(document);
nitindb0445972006-02-16 22:19:20 +0000198
nitindf3f10872007-09-03 06:36:56 +0000199 // make the entire document dirty
200 // this also happens on a "save as"
201 if (document != null && isInstalled() && fLastPartitions != null && document.getLength() == 0) {
202 /**
203 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=199053
204 *
205 * Process the strategies for the last known-good partitions.
206 */
207 for (int i = 0; i < fLastPartitions.length; i++) {
nitind69bc47f2008-04-07 22:45:51 +0000208 ValidatorStrategy validatorStrategy = getValidatorStrategy();
209 if (validatorStrategy != null) {
210 validatorStrategy.reconcile(fLastPartitions[i], createDirtyRegion(fLastPartitions[i], DirtyRegion.REMOVE));
211 }
nitindf3f10872007-09-03 06:36:56 +0000212 }
213 if (fSpellcheckStrategy != null) {
214 fSpellcheckStrategy.reconcile(new Region(0, document.getLength()));
215 }
216 }
217 }
nitindc77f9632008-03-19 21:26:39 +0000218
nitindb0445972006-02-16 22:19:20 +0000219 /**
220 * @see org.eclipse.wst.sse.ui.internal.reconcile.DirtyRegionProcessor#uninstall()
221 */
222 public void uninstall() {
223 if (isInstalled()) {
224
225 cancel();
226
nitindb0445972006-02-16 22:19:20 +0000227 IReconcilingStrategy validatorStrategy = getValidatorStrategy();
228
229 if (validatorStrategy != null) {
230 if (validatorStrategy instanceof IReleasable)
231 ((IReleasable) validatorStrategy).release();
232 }
nitind0f059552006-05-01 17:36:50 +0000233 if (fSpellcheckStrategy != null) {
234 fSpellcheckStrategy.setDocument(null);
235 }
nitindb0445972006-02-16 22:19:20 +0000236 }
237 super.uninstall();
238 }
paverydc8b3062005-10-24 20:43:42 +0000239}