david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2006 IBM Corporation and others. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.sse.ui.internal.contentassist; |
| 14 | |
nitind | 5c182ae | 2006-08-02 15:54:43 +0000 | [diff] [blame] | 15 | import org.eclipse.jface.text.ITextViewer; |
david_williams | b5d0563 | 2006-02-27 09:24:00 +0000 | [diff] [blame] | 16 | import org.eclipse.wst.sse.core.StructuredModelManager; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 17 | import org.eclipse.wst.sse.core.internal.provisional.IModelManager; |
| 18 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 19 | import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| 21 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | /** |
| 25 | * @author pavery |
| 26 | * |
| 27 | */ |
| 28 | public class ContentAssistUtils { |
| 29 | |
| 30 | /** |
| 31 | * Returns the closest IndexedRegion for the offset and viewer allowing |
| 32 | * for differences between viewer offsets and model positions. note: this |
| 33 | * method returns an IndexedRegion for read only |
| 34 | * |
| 35 | * @param viewer |
| 36 | * the viewer whose document is used to compute the proposals |
| 37 | * @param documentOffset |
| 38 | * an offset within the document for which completions should |
| 39 | * be computed |
| 40 | * @return an IndexedRegion |
| 41 | */ |
nitind | 5c182ae | 2006-08-02 15:54:43 +0000 | [diff] [blame] | 42 | public static IndexedRegion getNodeAt(ITextViewer viewer, int documentOffset) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 43 | |
| 44 | if (viewer == null) |
| 45 | return null; |
| 46 | |
| 47 | IndexedRegion node = null; |
nitind | ec3bcb6 | 2004-12-30 09:45:45 +0000 | [diff] [blame] | 48 | IModelManager mm = StructuredModelManager.getModelManager(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 49 | IStructuredModel model = null; |
| 50 | if (mm != null) |
| 51 | model = mm.getExistingModelForRead(viewer.getDocument()); |
| 52 | try { |
| 53 | if (model != null) { |
| 54 | int lastOffset = documentOffset; |
| 55 | node = model.getIndexedRegion(documentOffset); |
| 56 | while (node == null && lastOffset >= 0) { |
| 57 | lastOffset--; |
| 58 | node = model.getIndexedRegion(lastOffset); |
| 59 | } |
| 60 | } |
| 61 | } finally { |
| 62 | if (model != null) |
| 63 | model.releaseFromRead(); |
| 64 | } |
| 65 | return node; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the closest IStructuredDocumentRegion for the offest and |
| 70 | * viewer. |
| 71 | * |
| 72 | * @param viewer |
| 73 | * @param documentOffset |
| 74 | * @return the closest IStructuredDocumentRegion for the offest and |
| 75 | * viewer. |
| 76 | */ |
nitind | 5c182ae | 2006-08-02 15:54:43 +0000 | [diff] [blame] | 77 | public static IStructuredDocumentRegion getStructuredDocumentRegion(ITextViewer viewer, int documentOffset) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 78 | IStructuredDocumentRegion sdRegion = null; |
| 79 | if (viewer == null || viewer.getDocument() == null) |
| 80 | return null; |
| 81 | |
| 82 | int lastOffset = documentOffset; |
| 83 | IStructuredDocument doc = (IStructuredDocument) viewer.getDocument(); |
| 84 | sdRegion = doc.getRegionAtCharacterOffset(documentOffset); |
| 85 | while (sdRegion == null && lastOffset >= 0) { |
| 86 | lastOffset--; |
| 87 | sdRegion = doc.getRegionAtCharacterOffset(lastOffset); |
| 88 | } |
| 89 | return sdRegion; |
| 90 | } |
| 91 | |
| 92 | } |