blob: 2e2a79908fa622e072c5973c482b1a374dac6c1c [file] [log] [blame]
david_williamscfdb2cd2004-11-11 08:37:49 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2004 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 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
13package org.eclipse.wst.sse.ui.internal.contentassist;
14
david_williamscfdb2cd2004-11-11 08:37:49 +000015import org.eclipse.wst.sse.core.IModelManager;
david_williamscfdb2cd2004-11-11 08:37:49 +000016import org.eclipse.wst.sse.core.IStructuredModel;
17import org.eclipse.wst.sse.core.IndexedRegion;
david_williams425ffe72004-12-07 21:46:39 +000018import org.eclipse.wst.sse.core.StructuredModelManager;
david_williamscfdb2cd2004-11-11 08:37:49 +000019import org.eclipse.wst.sse.core.text.IStructuredDocument;
20import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion;
21import org.eclipse.wst.sse.ui.StructuredTextViewer;
22
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 */
42 public static IndexedRegion getNodeAt(StructuredTextViewer viewer, int documentOffset) {
43
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 */
77 public static IStructuredDocumentRegion getStructuredDocumentRegion(StructuredTextViewer viewer, int documentOffset) {
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}