nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 12 | package org.eclipse.wst.sse.ui.internal.reconcile; |
| 13 | |
| 14 | import java.io.ByteArrayInputStream; |
| 15 | import java.io.IOException; |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 18 | |
| 19 | import org.eclipse.core.runtime.Platform; |
| 20 | import org.eclipse.core.runtime.content.IContentDescription; |
| 21 | import org.eclipse.core.runtime.content.IContentType; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 22 | import org.eclipse.jface.text.IDocument; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 23 | import org.eclipse.jface.text.ITypedRegion; |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame^] | 24 | import org.eclipse.jface.text.Region; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 25 | import org.eclipse.jface.text.reconciler.DirtyRegion; |
| 26 | import org.eclipse.jface.text.reconciler.IReconcilingStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 27 | import org.eclipse.jface.text.source.ISourceViewer; |
| 28 | import org.eclipse.wst.sse.ui.internal.IReleasable; |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 29 | import org.eclipse.wst.sse.ui.internal.Logger; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 30 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorBuilder; |
| 31 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorMetaData; |
| 32 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorStrategy; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 33 | import org.eclipse.wst.sse.ui.internal.spelling.SpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 36 | * Adds to DirtyRegionProcessor Job: - IDocumentListener - ValidatorStrategy - |
| 37 | * Text viewer(dispose, input changed) listeners. - default, spelling, and |
| 38 | * validator strategies - DirtyRegion processing logic. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 39 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 40 | public class DocumentRegionProcessor extends DirtyRegionProcessor { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 41 | |
nitind | 19a7812 | 2006-09-27 20:23:56 +0000 | [diff] [blame] | 42 | private static final boolean DEBUG_VALIDATORS = Boolean.TRUE.toString().equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.sse.ui/debug/reconcilerValidators")); //$NON-NLS-1$ |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 43 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 44 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 45 | * A strategy to use the defined default Spelling service. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 46 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 47 | private SpellcheckStrategy fSpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 48 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 49 | /** |
| 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; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 55 | |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 56 | private final String SSE_UI_ID = "org.eclipse.wst.sse.ui"; //$NON-NLS-1$ |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 57 | |
amywu | cfaee2d | 2006-11-21 00:28:32 +0000 | [diff] [blame] | 58 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 59 | protected void beginProcessing() { |
| 60 | super.beginProcessing(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 61 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 62 | if (validatorStrategy != null) { |
| 63 | validatorStrategy.beginProcessing(); |
| 64 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 65 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 66 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 67 | protected void endProcessing() { |
| 68 | super.endProcessing(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 69 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 70 | if (validatorStrategy != null) { |
| 71 | validatorStrategy.endProcessing(); |
| 72 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 73 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 74 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 75 | protected String getContentType(IDocument doc) { |
| 76 | if (doc == null) |
| 77 | return null; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 78 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 79 | String contentTypeId = null; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 80 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 81 | // pa_TODO it would be nice to be able to get filename from |
| 82 | // IDocument... |
| 83 | // because it seems that getting content type from input stream |
| 84 | // isn't all that accurate (eg. w/ a javascript file) |
| 85 | |
| 86 | // IContentType ct = |
| 87 | // Platform.getContentTypeManager().findContentTypeFor("test.js"); |
| 88 | IContentType ct = null; |
| 89 | try { |
| 90 | IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(new ByteArrayInputStream(doc.get().getBytes()), null, IContentDescription.ALL); |
| 91 | if (desc != null) { |
| 92 | ct = desc.getContentType(); |
| 93 | if (ct != null) |
| 94 | contentTypeId = ct.getId(); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 97 | catch (IOException e) { |
| 98 | // just bail |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 99 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 100 | return contentTypeId; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 101 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 102 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 103 | protected final StructuredTextReconcilingStrategy getSpellcheckStrategy() { |
| 104 | if (fSpellcheckStrategy == null) { |
| 105 | String contentTypeId = getContentType(getDocument()); |
| 106 | if (contentTypeId != null) { |
| 107 | if (getTextViewer() instanceof ISourceViewer) { |
| 108 | ISourceViewer viewer = (ISourceViewer) getTextViewer(); |
| 109 | fSpellcheckStrategy = new SpellcheckStrategy(viewer, contentTypeId); |
| 110 | fSpellcheckStrategy.setDocument(getDocument()); |
| 111 | fSpellcheckStrategy.setDocumentPartitioning(getDocumentPartitioning()); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 114 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 115 | return fSpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 116 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 117 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 118 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 119 | * @return Returns the ValidatorStrategy. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 120 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 121 | protected ValidatorStrategy getValidatorStrategy() { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 122 | if (fValidatorStrategy == null) { |
| 123 | ValidatorStrategy validatorStrategy = null; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 124 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 125 | if (getTextViewer() instanceof ISourceViewer) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 126 | ISourceViewer viewer = (ISourceViewer) getTextViewer(); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 127 | String contentTypeId = null; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 128 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 129 | IDocument doc = viewer.getDocument(); |
| 130 | contentTypeId = getContentType(doc); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 131 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 132 | if (contentTypeId != null) { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 133 | validatorStrategy = new ValidatorStrategy(viewer, contentTypeId); |
| 134 | ValidatorBuilder vBuilder = new ValidatorBuilder(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 135 | ValidatorMetaData[] vmds = vBuilder.getValidatorMetaData(SSE_UI_ID); |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 136 | List enabledValidators = new ArrayList(1); |
| 137 | /* if any "must" handle this content type, just add them */ |
| 138 | boolean foundSpecificContentTypeValidators = false; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 139 | for (int i = 0; i < vmds.length; i++) { |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 140 | if (vmds[i].mustHandleContentType(contentTypeId)) { |
| 141 | if (DEBUG_VALIDATORS) |
| 142 | Logger.log(Logger.INFO, contentTypeId + " using specific validator " + vmds[i].getValidatorId()); //$NON-NLS-1$ |
| 143 | foundSpecificContentTypeValidators = true; |
| 144 | enabledValidators.add(vmds[i]); |
| 145 | } |
| 146 | } |
| 147 | if (!foundSpecificContentTypeValidators) { |
| 148 | for (int i = 0; i < vmds.length; i++) { |
| 149 | if (vmds[i].canHandleContentType(contentTypeId)) { |
| 150 | if (DEBUG_VALIDATORS) |
| 151 | Logger.log(Logger.INFO, contentTypeId + " using inherited(?) validator " + vmds[i].getValidatorId()); //$NON-NLS-1$ |
| 152 | enabledValidators.add(vmds[i]); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | for (int i = 0; i < enabledValidators.size(); i++) { |
| 157 | validatorStrategy.addValidatorMetaData((ValidatorMetaData) enabledValidators.get(i)); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | fValidatorStrategy = validatorStrategy; |
| 162 | } |
| 163 | return fValidatorStrategy; |
| 164 | } |
| 165 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 166 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 167 | * @param dirtyRegion |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 168 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 169 | protected void process(DirtyRegion dirtyRegion) { |
amywu | cfaee2d | 2006-11-21 00:28:32 +0000 | [diff] [blame] | 170 | if (!isInstalled() || isInRewriteSession()) |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 171 | return; |
| 172 | |
| 173 | super.process(dirtyRegion); |
| 174 | |
| 175 | // Also call the validation and spell-check strategies |
| 176 | ITypedRegion[] partitions = computePartitioning(dirtyRegion); |
| 177 | |
| 178 | DirtyRegion dirty = null; |
| 179 | for (int i = 0; i < partitions.length; i++) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 180 | // [source]validator (extension) for this partition |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 181 | if (getValidatorStrategy() != null) { |
| 182 | dirty = createDirtyRegion(partitions[i], DirtyRegion.INSERT); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 183 | getValidatorStrategy().reconcile(partitions[i], dirty); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 184 | } |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame] | 185 | } |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 186 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 187 | // single spell-check for everything |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 188 | if (getSpellcheckStrategy() != null) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 189 | getSpellcheckStrategy().reconcile(dirtyRegion, dirtyRegion); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 190 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 191 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 192 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 193 | public void setDocument(IDocument doc) { |
| 194 | super.setDocument(doc); |
| 195 | IReconcilingStrategy validatorStrategy = getValidatorStrategy(); |
| 196 | if (validatorStrategy != null) { |
| 197 | validatorStrategy.setDocument(doc); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 198 | } |
nitind | 0f05955 | 2006-05-01 17:36:50 +0000 | [diff] [blame] | 199 | if (fSpellcheckStrategy != null) { |
| 200 | fSpellcheckStrategy.setDocument(doc); |
| 201 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 202 | } |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame^] | 203 | |
| 204 | protected void setEntireDocumentDirty(IDocument document) { |
| 205 | super.setEntireDocumentDirty(document); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 206 | |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame^] | 207 | // make the entire document dirty |
| 208 | // this also happens on a "save as" |
| 209 | if (document != null && isInstalled() && fLastPartitions != null && document.getLength() == 0) { |
| 210 | /** |
| 211 | * https://bugs.eclipse.org/bugs/show_bug.cgi?id=199053 |
| 212 | * |
| 213 | * Process the strategies for the last known-good partitions. |
| 214 | */ |
| 215 | for (int i = 0; i < fLastPartitions.length; i++) { |
| 216 | getValidatorStrategy().reconcile(fLastPartitions[i], createDirtyRegion(fLastPartitions[i], DirtyRegion.REMOVE)); |
| 217 | } |
| 218 | if (fSpellcheckStrategy != null) { |
| 219 | fSpellcheckStrategy.reconcile(new Region(0, document.getLength())); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 224 | /** |
| 225 | * @see org.eclipse.wst.sse.ui.internal.reconcile.DirtyRegionProcessor#uninstall() |
| 226 | */ |
| 227 | public void uninstall() { |
| 228 | if (isInstalled()) { |
| 229 | |
| 230 | cancel(); |
| 231 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 232 | IReconcilingStrategy validatorStrategy = getValidatorStrategy(); |
| 233 | |
| 234 | if (validatorStrategy != null) { |
| 235 | if (validatorStrategy instanceof IReleasable) |
| 236 | ((IReleasable) validatorStrategy).release(); |
| 237 | } |
nitind | 0f05955 | 2006-05-01 17:36:50 +0000 | [diff] [blame] | 238 | if (fSpellcheckStrategy != null) { |
| 239 | fSpellcheckStrategy.setDocument(null); |
| 240 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 241 | } |
| 242 | super.uninstall(); |
| 243 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 244 | } |