blob: 275f554b8a6b6077791d8e76e7fa40d0ca63e60b [file] [log] [blame]
david_williamscfdb2cd2004-11-11 08:37:49 +00001/*******************************************************************************
amywuecebb042007-04-10 20:07:35 +00002 * Copyright (c) 2001, 2006 IBM Corporation and others.
david_williamscfdb2cd2004-11-11 08:37:49 +00003 * 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
amywuecebb042007-04-10 20:07:35 +00007 *
david_williamscfdb2cd2004-11-11 08:37:49 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
13package org.eclipse.wst.sse.ui.internal.contentassist;
14
nitind5c182ae2006-08-02 15:54:43 +000015import org.eclipse.jface.text.ITextViewer;
david_williamsb5d05632006-02-27 09:24:00 +000016import org.eclipse.wst.sse.core.StructuredModelManager;
david_williams4ad020f2005-04-18 08:00:30 +000017import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
18import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
19import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
david_williams4ad020f2005-04-18 08:00:30 +000020import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
21import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
david_williamscfdb2cd2004-11-11 08:37:49 +000022
23
24/**
25 * @author pavery
26 *
27 */
28public 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 */
nitind5c182ae2006-08-02 15:54:43 +000042 public static IndexedRegion getNodeAt(ITextViewer viewer, int documentOffset) {
david_williamscfdb2cd2004-11-11 08:37:49 +000043
44 if (viewer == null)
45 return null;
46
47 IndexedRegion node = null;
nitindec3bcb62004-12-30 09:45:45 +000048 IModelManager mm = StructuredModelManager.getModelManager();
david_williamscfdb2cd2004-11-11 08:37:49 +000049 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 */
nitind5c182ae2006-08-02 15:54:43 +000077 public static IStructuredDocumentRegion getStructuredDocumentRegion(ITextViewer viewer, int documentOffset) {
david_williamscfdb2cd2004-11-11 08:37:49 +000078 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}