Skip to content

Commit 127a7ee

Browse files
author
angelozerr
committed
Implement TypeScript "Find References" by copying/pasting File Search.
See #44
1 parent 5799f0e commit 127a7ee

16 files changed

+1559
-76
lines changed

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/core/utils/WorkbenchResourceUtil.java

+35
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
*/
1111
package ts.eclipse.ide.core.utils;
1212

13+
import java.io.File;
14+
1315
import org.eclipse.core.resources.IContainer;
1416
import org.eclipse.core.resources.IFile;
1517
import org.eclipse.core.resources.IResource;
18+
import org.eclipse.core.resources.IWorkspaceRoot;
19+
import org.eclipse.core.resources.ResourcesPlugin;
1620
import org.eclipse.core.runtime.CoreException;
1721
import org.eclipse.core.runtime.IPath;
22+
import org.eclipse.core.runtime.Path;
23+
24+
import ts.utils.StringUtils;
1825

1926
/**
2027
* Utilities for Eclipse resources.
@@ -45,4 +52,32 @@ public static IFile findFileInContainerOrParent(IContainer container, IPath name
4552
}
4653
return findFileInContainerOrParent(container.getParent(), name);
4754
}
55+
56+
public static IFile findFileFromWorkspace(String path) {
57+
if (StringUtils.isEmpty(path)) {
58+
return null;
59+
}
60+
IPath filePath = new Path(path);
61+
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
62+
IFile file = root.getFile(filePath);
63+
if (file.exists()) {
64+
return file;
65+
}
66+
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(filePath);
67+
if (files.length > 0) {
68+
file = files[0];
69+
if (file.exists()) {
70+
return file;
71+
}
72+
}
73+
return null;
74+
}
75+
76+
public static File findFileFormFileSystem(String path) {
77+
if (StringUtils.isEmpty(path)) {
78+
return null;
79+
}
80+
File file = new File(path);
81+
return (file.exists() && file.isFile()) ? file : null;
82+
}
4883
}

eclipse/ts.eclipse.ide.ui/plugin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
point="org.eclipse.search.searchResultViewPages">
216216
<viewPage
217217
id="ts.eclipse.ide.ui.search.TypeScriptSearchResultPage"
218-
searchResultClass="ts.eclipse.ide.ui.search.TypeScriptSearchResultPage"
218+
searchResultClass="ts.eclipse.ide.ui.search.TypeScriptSearchResult"
219219
class="ts.eclipse.ide.internal.ui.search.TypeScriptSearchResultPage">
220220
</viewPage>
221221
</extension>

eclipse/ts.eclipse.ide.ui/src/ts/eclipse/ide/internal/ui/TypeScriptUIMessages.java

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class TypeScriptUIMessages extends NLS {
6868

6969
// Search
7070
public static String TypeScriptSearchQuery_label;
71+
public static String TypeScriptSearchQuery_result;
7172

7273

7374
public static ResourceBundle getResourceBundle() {

eclipse/ts.eclipse.ide.ui/src/ts/eclipse/ide/internal/ui/TypeScriptUIMessages.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ CompilerConfigurationBlock_embedded_checkbox_label=Embedded TypeScript
4848
CompilerConfigurationBlock_installed_checkbox_label=Installed TypeScript
4949

5050
# Search
51-
TypeScriptSearchQuery_label=TypeScript Search
51+
TypeScriptSearchQuery_label=TypeScript Search
52+
TypeScriptSearchQuery_result= {0} matches - done in {1} ms.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2000, 2010 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+
package ts.eclipse.ide.internal.ui.search;
12+
13+
import org.eclipse.swt.SWT;
14+
import org.eclipse.swt.custom.StyleRange;
15+
import org.eclipse.swt.widgets.Display;
16+
17+
import org.eclipse.jface.preference.JFacePreferences;
18+
import org.eclipse.jface.resource.JFaceResources;
19+
import org.eclipse.jface.util.IPropertyChangeListener;
20+
import org.eclipse.jface.util.PropertyChangeEvent;
21+
import org.eclipse.jface.viewers.ColumnViewer;
22+
import org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider;
23+
import org.eclipse.jface.viewers.ILabelProvider;
24+
import org.eclipse.jface.viewers.StyledString;
25+
import org.eclipse.jface.viewers.StyledString.Styler;
26+
import org.eclipse.jface.viewers.ViewerColumn;
27+
28+
import org.eclipse.ui.IWorkbenchPreferenceConstants;
29+
import org.eclipse.ui.PlatformUI;
30+
31+
32+
public class DecoratingTypeScriptSearchLabelProvider extends DecoratingStyledCellLabelProvider implements IPropertyChangeListener, ILabelProvider {
33+
34+
private static final String HIGHLIGHT_BG_COLOR_NAME= "org.eclipse.search.ui.match.highlight"; //$NON-NLS-1$
35+
36+
public static final Styler HIGHLIGHT_STYLE= StyledString.createColorRegistryStyler(null, HIGHLIGHT_BG_COLOR_NAME);
37+
38+
public DecoratingTypeScriptSearchLabelProvider(TypeScriptLabelProvider provider) {
39+
super(provider, PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator(), null);
40+
}
41+
42+
public void initialize(ColumnViewer viewer, ViewerColumn column) {
43+
PlatformUI.getPreferenceStore().addPropertyChangeListener(this);
44+
JFaceResources.getColorRegistry().addListener(this);
45+
46+
setOwnerDrawEnabled(showColoredLabels());
47+
48+
super.initialize(viewer, column);
49+
}
50+
51+
public void dispose() {
52+
super.dispose();
53+
PlatformUI.getPreferenceStore().removePropertyChangeListener(this);
54+
JFaceResources.getColorRegistry().removeListener(this);
55+
}
56+
57+
private void refresh() {
58+
ColumnViewer viewer= getViewer();
59+
60+
if (viewer == null) {
61+
return;
62+
}
63+
boolean showColoredLabels= showColoredLabels();
64+
if (showColoredLabels != isOwnerDrawEnabled()) {
65+
setOwnerDrawEnabled(showColoredLabels);
66+
viewer.refresh();
67+
} else if (showColoredLabels) {
68+
viewer.refresh();
69+
}
70+
}
71+
72+
protected StyleRange prepareStyleRange(StyleRange styleRange, boolean applyColors) {
73+
if (!applyColors && styleRange.background != null) {
74+
styleRange= super.prepareStyleRange(styleRange, applyColors);
75+
styleRange.borderStyle= SWT.BORDER_DOT;
76+
return styleRange;
77+
}
78+
return super.prepareStyleRange(styleRange, applyColors);
79+
}
80+
81+
public static boolean showColoredLabels() {
82+
return PlatformUI.getPreferenceStore().getBoolean(IWorkbenchPreferenceConstants.USE_COLORED_LABELS);
83+
}
84+
85+
public void propertyChange(PropertyChangeEvent event) {
86+
String property= event.getProperty();
87+
if (property.equals(JFacePreferences.QUALIFIER_COLOR) || property.equals(JFacePreferences.COUNTER_COLOR) || property.equals(JFacePreferences.DECORATIONS_COLOR)
88+
|| property.equals(HIGHLIGHT_BG_COLOR_NAME) || property.equals(IWorkbenchPreferenceConstants.USE_COLORED_LABELS)) {
89+
Display.getDefault().asyncExec(new Runnable() {
90+
public void run() {
91+
refresh();
92+
}
93+
});
94+
}
95+
}
96+
97+
public String getText(Object element) {
98+
return getStyledText(element).getString();
99+
}
100+
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2000, 2008 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+
package ts.eclipse.ide.internal.ui.search;
12+
13+
import java.util.HashMap;
14+
15+
import org.eclipse.core.runtime.CoreException;
16+
17+
import org.eclipse.core.resources.IFile;
18+
import org.eclipse.core.resources.IMarker;
19+
20+
import org.eclipse.ui.IEditorDescriptor;
21+
import org.eclipse.ui.IEditorInput;
22+
import org.eclipse.ui.IEditorPart;
23+
import org.eclipse.ui.IEditorReference;
24+
import org.eclipse.ui.IEditorRegistry;
25+
import org.eclipse.ui.IReusableEditor;
26+
import org.eclipse.ui.IWorkbenchPage;
27+
import org.eclipse.ui.PartInitException;
28+
import org.eclipse.ui.ide.IDE;
29+
import org.eclipse.ui.part.FileEditorInput;
30+
31+
import org.eclipse.ui.texteditor.ITextEditor;
32+
33+
import org.eclipse.search.internal.ui.SearchMessages;
34+
import org.eclipse.search.internal.ui.SearchPlugin;
35+
import org.eclipse.search.ui.NewSearchUI;
36+
37+
public class EditorOpener {
38+
39+
private IEditorReference fReusedEditor;
40+
41+
public IEditorPart open(IWorkbenchPage wbPage, IFile file, boolean activate) throws PartInitException {
42+
if (NewSearchUI.reuseEditor())
43+
return showWithReuse(file, wbPage, getEditorID(file), activate);
44+
return showWithoutReuse(file, wbPage, getEditorID(file), activate);
45+
}
46+
47+
public IEditorPart openAndSelect(IWorkbenchPage wbPage, IFile file, int offset, int length, boolean activate) throws PartInitException {
48+
String editorId= null;
49+
IEditorDescriptor desc= IDE.getEditorDescriptor(file);
50+
if (desc == null || !desc.isInternal()) {
51+
editorId= "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$
52+
} else {
53+
editorId= desc.getId();
54+
}
55+
56+
IEditorPart editor;
57+
if (NewSearchUI.reuseEditor()) {
58+
editor= showWithReuse(file, wbPage, editorId, activate);
59+
} else {
60+
editor= showWithoutReuse(file, wbPage, editorId, activate);
61+
}
62+
63+
if (editor instanceof ITextEditor) {
64+
ITextEditor textEditor= (ITextEditor) editor;
65+
textEditor.selectAndReveal(offset, length);
66+
} else if (editor != null) {
67+
showWithMarker(editor, file, offset, length);
68+
}
69+
return editor;
70+
}
71+
72+
private IEditorPart showWithoutReuse(IFile file, IWorkbenchPage wbPage, String editorID, boolean activate) throws PartInitException {
73+
return IDE.openEditor(wbPage, file, editorID, activate);
74+
}
75+
76+
77+
private String getEditorID(IFile file) throws PartInitException {
78+
IEditorDescriptor desc= IDE.getEditorDescriptor(file);
79+
if (desc == null)
80+
return SearchPlugin.getDefault().getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID).getId();
81+
return desc.getId();
82+
}
83+
84+
private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException {
85+
IEditorInput input= new FileEditorInput(file);
86+
IEditorPart editor= page.findEditor(input);
87+
if (editor != null) {
88+
page.bringToTop(editor);
89+
if (activate) {
90+
page.activate(editor);
91+
}
92+
return editor;
93+
}
94+
IEditorReference reusedEditorRef= fReusedEditor;
95+
if (reusedEditorRef != null) {
96+
boolean isOpen= reusedEditorRef.getEditor(false) != null;
97+
boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
98+
if (canBeReused) {
99+
boolean showsSameInputType= reusedEditorRef.getId().equals(editorId);
100+
if (!showsSameInputType) {
101+
page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
102+
fReusedEditor= null;
103+
} else {
104+
editor= reusedEditorRef.getEditor(true);
105+
if (editor instanceof IReusableEditor) {
106+
((IReusableEditor) editor).setInput(input);
107+
page.bringToTop(editor);
108+
if (activate) {
109+
page.activate(editor);
110+
}
111+
return editor;
112+
}
113+
}
114+
}
115+
}
116+
editor= page.openEditor(input, editorId, activate);
117+
if (editor instanceof IReusableEditor) {
118+
IEditorReference reference= (IEditorReference) page.getReference(editor);
119+
fReusedEditor= reference;
120+
} else {
121+
fReusedEditor= null;
122+
}
123+
return editor;
124+
}
125+
126+
private void showWithMarker(IEditorPart editor, IFile file, int offset, int length) throws PartInitException {
127+
IMarker marker= null;
128+
try {
129+
marker= file.createMarker(NewSearchUI.SEARCH_MARKER);
130+
HashMap attributes= new HashMap(4);
131+
attributes.put(IMarker.CHAR_START, new Integer(offset));
132+
attributes.put(IMarker.CHAR_END, new Integer(offset + length));
133+
marker.setAttributes(attributes);
134+
IDE.gotoMarker(editor, marker);
135+
} catch (CoreException e) {
136+
throw new PartInitException(SearchMessages.FileSearchPage_error_marker, e);
137+
} finally {
138+
if (marker != null)
139+
try {
140+
marker.delete();
141+
} catch (CoreException e) {
142+
// ignore
143+
}
144+
}
145+
}
146+
147+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2000, 2005 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+
package ts.eclipse.ide.internal.ui.search;
12+
13+
public interface ITypeScriptSearchContentProvider {
14+
15+
public abstract void elementsChanged(Object[] updatedElements);
16+
17+
public abstract void clear();
18+
19+
}

0 commit comments

Comments
 (0)