Skip to content

Commit 663e57f

Browse files
committed
Initialize refactoring classes like JDT refactoring. See
#28
1 parent c0061f1 commit 663e57f

20 files changed

+1766
-71
lines changed

eclipse/jsdt/ts.eclipse.ide.jsdt.core/META-INF/MANIFEST.MF

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ Require-Bundle: org.eclipse.core.runtime,
1414
org.eclipse.jface.text,
1515
org.eclipse.wst.sse.core,
1616
org.eclipse.wst.xml.core,
17-
org.eclipse.wst.sse.ui
17+
org.eclipse.wst.sse.ui,
18+
org.eclipse.ltk.core.refactoring,
19+
ts.eclipse.ide.core
1820
Bundle-ActivationPolicy: lazy
1921
Bundle-Activator: ts.eclipse.ide.jsdt.core.JSDTTypeScriptCorePlugin
2022
Import-Package: com.eclipsesource.json;version="[0.9.4,0.9.5)"
2123
Export-Package: ts.eclipse.ide.jsdt.core,
24+
ts.eclipse.ide.jsdt.core.refactoring,
2225
ts.eclipse.ide.jsdt.core.template
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package ts.eclipse.ide.jsdt.core.refactoring;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.TimeUnit;
6+
7+
import org.eclipse.core.resources.IFile;
8+
import org.eclipse.core.runtime.Assert;
9+
import org.eclipse.core.runtime.CoreException;
10+
import org.eclipse.core.runtime.IProgressMonitor;
11+
import org.eclipse.core.runtime.IStatus;
12+
import org.eclipse.core.runtime.OperationCanceledException;
13+
import org.eclipse.core.runtime.Status;
14+
import org.eclipse.ltk.core.refactoring.Change;
15+
import org.eclipse.ltk.core.refactoring.CompositeChange;
16+
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
17+
import org.eclipse.ltk.core.refactoring.TextFileChange;
18+
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
19+
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
20+
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
21+
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
22+
import org.eclipse.text.edits.MultiTextEdit;
23+
import org.eclipse.text.edits.ReplaceEdit;
24+
25+
import ts.client.TextSpan;
26+
import ts.client.rename.RenameResponseBody;
27+
import ts.client.rename.SpanGroup;
28+
import ts.eclipse.ide.core.utils.WorkbenchResourceUtil;
29+
import ts.eclipse.ide.jsdt.core.JSDTTypeScriptCorePlugin;
30+
import ts.resources.ITypeScriptFile;
31+
32+
public class TypeScriptRenameProcessor extends RenameProcessor {
33+
34+
private final ITypeScriptFile tsFile;
35+
private final int offset;
36+
private final String oldName;
37+
38+
private String newName;
39+
40+
public TypeScriptRenameProcessor(ITypeScriptFile tsFile, int offset, String oldName) {
41+
this.tsFile = tsFile;
42+
this.offset = offset;
43+
this.oldName = oldName;
44+
}
45+
46+
@Override
47+
public Object[] getElements() {
48+
return null;
49+
}
50+
51+
@Override
52+
public String getIdentifier() {
53+
return "ts.eclipse.ide.core.refactoring.rename";
54+
}
55+
56+
@Override
57+
public String getProcessorName() {
58+
return "Rename TypeScript Element";
59+
}
60+
61+
@Override
62+
public boolean isApplicable() throws CoreException {
63+
return true;
64+
}
65+
66+
@Override
67+
public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
68+
throws CoreException, OperationCanceledException {
69+
return new RefactoringStatus();
70+
}
71+
72+
@Override
73+
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context)
74+
throws CoreException, OperationCanceledException {
75+
return new RefactoringStatus();
76+
}
77+
78+
@Override
79+
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
80+
try {
81+
RenameResponseBody rename = tsFile.rename(offset, false, false).get(1000, TimeUnit.MILLISECONDS);
82+
List<SpanGroup> locs = rename.getLocs();
83+
84+
List<Change> fileChanges = new ArrayList<>();
85+
for (SpanGroup loc : locs) {
86+
IFile file = WorkbenchResourceUtil.findFileFromWorkspace(loc.getFile());
87+
TextFileChange change = new TextFileChange(file.getName(), file);
88+
change.setEdit(new MultiTextEdit());
89+
change.setTextType("ts");
90+
91+
List<TextSpan> spans = loc.getLocs();
92+
for (TextSpan textSpan : spans) {
93+
int start = tsFile.getPosition(textSpan.getStart());
94+
int end = tsFile.getPosition(textSpan.getEnd());
95+
int length = end - start;
96+
ReplaceEdit edit = new ReplaceEdit(start, length, this.newName);
97+
change.addEdit(edit);
98+
}
99+
fileChanges.add(change);
100+
}
101+
return new CompositeChange("Rename TypeScript Element",
102+
fileChanges.toArray(new Change[fileChanges.size()]));
103+
} catch (OperationCanceledException e) {
104+
throw e;
105+
} catch (Exception e) {
106+
throw new CoreException(new Status(IStatus.ERROR, JSDTTypeScriptCorePlugin.PLUGIN_ID, "Error while rename", e));
107+
}
108+
}
109+
110+
@Override
111+
public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants)
112+
throws CoreException {
113+
return null;
114+
}
115+
116+
public String getOldName() {
117+
return this.oldName;
118+
}
119+
120+
public void setNewName(String newName) {
121+
Assert.isNotNull(newName);
122+
this.newName = newName;
123+
}
124+
125+
public int getSaveMode() {
126+
return 1; //RefactoringSaveHelper.SAVE_NOTHING;
127+
}
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2005, 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.jsdt.core.refactoring;
12+
13+
import org.eclipse.core.runtime.Assert;
14+
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
15+
import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
16+
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
17+
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
18+
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
19+
import org.eclipse.wst.jsdt.internal.corext.refactoring.RefactoringCoreMessages;
20+
import org.eclipse.wst.jsdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
21+
import org.eclipse.wst.jsdt.internal.corext.util.Messages;
22+
23+
/**
24+
* A rename refactoring which can be initialized with refactoring arguments.
25+
*
26+
*
27+
*/
28+
public final class TypeScriptRenameRefactoring extends RenameRefactoring implements IScriptableRefactoring {
29+
30+
/**
31+
* Creates a new java rename refactoring.
32+
*
33+
* @param processor
34+
* the rename processor to use
35+
*/
36+
public TypeScriptRenameRefactoring(final RenameProcessor processor) {
37+
super(processor);
38+
}
39+
40+
/**
41+
* {@inheritDoc}
42+
*/
43+
public RefactoringStatus initialize(final RefactoringArguments arguments) {
44+
Assert.isNotNull(arguments);
45+
final RefactoringProcessor processor= getProcessor();
46+
if (processor instanceof IScriptableRefactoring) {
47+
return ((IScriptableRefactoring) processor).initialize(arguments);
48+
}
49+
return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.ProcessorBasedRefactoring_error_unsupported_initialization, getProcessor().getIdentifier()));
50+
}
51+
}

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/JSDTTypeScriptUIMessages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public class JSDTTypeScriptUIMessages extends NLS {
122122
public static String RenameInformationPopup_snap_under_left;
123123
public static String RenameInformationPopup_snap_under_right;
124124
public static String RenameInformationPopup_SnapTo;
125+
126+
public static String RenameSupport_not_available;
127+
128+
public static String RenameSupport_dialog_title;
125129

126130
public static ResourceBundle getResourceBundle() {
127131
try {

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/actions/RenameTypeScriptElementAction.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ts.eclipse.ide.jsdt.internal.ui.actions;
22

3+
import org.eclipse.jface.preference.IPreferenceStore;
34
import org.eclipse.jface.text.ITextSelection;
45
import org.eclipse.jface.viewers.IStructuredSelection;
56
import org.eclipse.ui.IWorkbenchSite;
7+
import org.eclipse.wst.jsdt.internal.ui.JavaScriptPlugin;
8+
import org.eclipse.wst.jsdt.ui.PreferenceConstants;
69
import org.eclipse.wst.jsdt.ui.actions.SelectionDispatchAction;
710

811
import ts.eclipse.ide.jsdt.internal.ui.editor.TypeScriptEditor;
9-
import ts.eclipse.ide.jsdt.internal.ui.refactoring.reorg.RenameLinkedMode;
12+
import ts.eclipse.ide.jsdt.internal.ui.refactoring.RenameLinkedMode;
1013

1114
public class RenameTypeScriptElementAction extends SelectionDispatchAction {
1215

@@ -68,8 +71,8 @@ private void doRun(ITextSelection selection) {
6871
}
6972
}
7073

71-
//IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
72-
boolean lightweight= true; //store.getBoolean(PreferenceConstants.REFACTOR_LIGHTWEIGHT);
74+
IPreferenceStore store= JavaScriptPlugin.getDefault().getPreferenceStore();
75+
boolean lightweight= store.getBoolean(PreferenceConstants.REFACTOR_LIGHTWEIGHT);
7376
run(selection, lightweight);
7477

7578
// try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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.jsdt.internal.ui.actions;
12+
13+
import java.lang.reflect.InvocationTargetException;
14+
15+
import org.eclipse.core.resources.IWorkspaceRunnable;
16+
import org.eclipse.core.resources.ResourcesPlugin;
17+
import org.eclipse.core.runtime.CoreException;
18+
import org.eclipse.core.runtime.IProgressMonitor;
19+
import org.eclipse.core.runtime.IStatus;
20+
import org.eclipse.core.runtime.OperationCanceledException;
21+
import org.eclipse.core.runtime.Status;
22+
import org.eclipse.core.runtime.jobs.ISchedulingRule;
23+
import org.eclipse.core.runtime.jobs.Job;
24+
import org.eclipse.jface.operation.IRunnableWithProgress;
25+
import org.eclipse.jface.operation.IThreadListener;
26+
import org.eclipse.wst.jsdt.core.JavaScriptCore;
27+
import org.eclipse.wst.jsdt.internal.ui.JavaUIStatus;
28+
29+
/**
30+
* An <code>IRunnableWithProgress</code> that adapts and <code>IWorkspaceRunnable</code>
31+
* so that is can be executed inside <code>IRunnableContext</code>. <code>OperationCanceledException</code>
32+
* thrown by the adapted runnable are caught and re-thrown as a <code>InterruptedException</code>.
33+
*/
34+
public class WorkbenchRunnableAdapter implements IRunnableWithProgress, IThreadListener {
35+
36+
private boolean fTransfer= false;
37+
private IWorkspaceRunnable fWorkspaceRunnable;
38+
private ISchedulingRule fRule;
39+
40+
/**
41+
* Runs a workspace runnable with the workspace lock.
42+
*/
43+
public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
44+
this(runnable, ResourcesPlugin.getWorkspace().getRoot());
45+
}
46+
47+
/**
48+
* Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
49+
*/
50+
public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) {
51+
fWorkspaceRunnable= runnable;
52+
fRule= rule;
53+
}
54+
55+
/**
56+
* Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
57+
* @param transfer <code>true</code> if the rule is to be transfered
58+
* to the model context thread. Otherwise <code>false</code>
59+
*/
60+
public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
61+
fWorkspaceRunnable= runnable;
62+
fRule= rule;
63+
fTransfer= transfer;
64+
}
65+
66+
public ISchedulingRule getSchedulingRule() {
67+
return fRule;
68+
}
69+
70+
/**
71+
* {@inheritDoc}
72+
*/
73+
public void threadChange(Thread thread) {
74+
if (fTransfer)
75+
Job.getJobManager().transferRule(fRule, thread);
76+
}
77+
78+
/*
79+
* @see IRunnableWithProgress#run(IProgressMonitor)
80+
*/
81+
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
82+
try {
83+
JavaScriptCore.run(fWorkspaceRunnable, fRule, monitor);
84+
} catch (OperationCanceledException e) {
85+
throw new InterruptedException(e.getMessage());
86+
} catch (CoreException e) {
87+
throw new InvocationTargetException(e);
88+
}
89+
}
90+
91+
public void runAsUserJob(String name, final Object jobFamiliy) {
92+
Job buildJob = new Job(name){
93+
/* (non-Javadoc)
94+
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
95+
*/
96+
protected IStatus run(IProgressMonitor monitor) {
97+
try {
98+
WorkbenchRunnableAdapter.this.run(monitor);
99+
} catch (InvocationTargetException e) {
100+
Throwable cause= e.getCause();
101+
if (cause instanceof CoreException) {
102+
return ((CoreException) cause).getStatus();
103+
} else {
104+
return JavaUIStatus.createError(IStatus.ERROR, cause);
105+
}
106+
} catch (InterruptedException e) {
107+
return Status.CANCEL_STATUS;
108+
} finally {
109+
monitor.done();
110+
}
111+
return Status.OK_STATUS;
112+
}
113+
public boolean belongsTo(Object family) {
114+
return jobFamiliy == family;
115+
}
116+
};
117+
buildJob.setRule(fRule);
118+
buildJob.setUser(true);
119+
buildJob.schedule();
120+
121+
// TODO: should block until user pressed 'to background'
122+
}
123+
}

0 commit comments

Comments
 (0)