7
7
*
8
8
* Contributors:
9
9
* Angelo Zerr <[email protected] > - initial API and implementation
10
+ * Lorenzo Dalla Vecchia <[email protected] > - added save actions
10
11
*/
11
12
package ts .eclipse .ide .jsdt .internal .ui .editor ;
12
13
14
+ import java .lang .reflect .InvocationTargetException ;
15
+ import java .util .ArrayList ;
16
+ import java .util .List ;
17
+
18
+ import org .eclipse .core .resources .IFile ;
19
+ import org .eclipse .core .resources .IProject ;
20
+ import org .eclipse .core .resources .ProjectScope ;
21
+ import org .eclipse .core .resources .ResourcesPlugin ;
13
22
import org .eclipse .core .runtime .CoreException ;
23
+ import org .eclipse .core .runtime .IProgressMonitor ;
24
+ import org .eclipse .core .runtime .IStatus ;
25
+ import org .eclipse .core .runtime .NullProgressMonitor ;
26
+ import org .eclipse .core .runtime .Status ;
27
+ import org .eclipse .core .runtime .SubMonitor ;
28
+ import org .eclipse .core .runtime .preferences .DefaultScope ;
29
+ import org .eclipse .core .runtime .preferences .InstanceScope ;
30
+ import org .eclipse .jface .preference .IPreferenceStore ;
14
31
import org .eclipse .jface .text .IDocument ;
32
+ import org .eclipse .ltk .core .refactoring .Change ;
33
+ import org .eclipse .ltk .core .refactoring .CompositeChange ;
34
+ import org .eclipse .ltk .core .refactoring .IUndoManager ;
35
+ import org .eclipse .ltk .core .refactoring .PerformChangeOperation ;
36
+ import org .eclipse .ltk .core .refactoring .RefactoringCore ;
37
+ import org .eclipse .ltk .core .refactoring .TextFileChange ;
38
+ import org .eclipse .ui .IFileEditorInput ;
15
39
import org .eclipse .ui .editors .text .ForwardingDocumentProvider ;
16
40
import org .eclipse .ui .editors .text .TextFileDocumentProvider ;
41
+ import org .eclipse .ui .preferences .ScopedPreferenceStore ;
42
+ import org .eclipse .ui .texteditor .ChainedPreferenceStore ;
17
43
import org .eclipse .ui .texteditor .IDocumentProvider ;
18
44
import org .eclipse .wst .jsdt .internal .ui .javaeditor .JavaDocumentSetupParticipant ;
19
45
import org .eclipse .wst .jsdt .ui .text .IJavaScriptPartitions ;
20
46
47
+ import ts .client .CodeEdit ;
48
+ import ts .eclipse .ide .core .TypeScriptCorePlugin ;
49
+ import ts .eclipse .ide .core .resources .IIDETypeScriptFile ;
50
+ import ts .eclipse .ide .core .resources .IIDETypeScriptProject ;
51
+ import ts .eclipse .ide .core .utils .DocumentUtils ;
52
+ import ts .eclipse .ide .core .utils .TypeScriptResourceUtil ;
53
+ import ts .eclipse .ide .jsdt .internal .ui .JSDTTypeScriptUIPlugin ;
54
+ import ts .eclipse .ide .ui .TypeScriptUIPlugin ;
55
+ import ts .eclipse .ide .ui .preferences .TypeScriptUIPreferenceConstants ;
56
+
21
57
public class TypeScriptDocumentProvider extends TextFileDocumentProvider {
22
58
23
59
public TypeScriptDocumentProvider () {
@@ -26,11 +62,115 @@ public TypeScriptDocumentProvider() {
26
62
new JavaDocumentSetupParticipant (), provider );
27
63
setParentDocumentProvider (provider );
28
64
}
29
-
65
+
30
66
@ Override
31
67
protected DocumentProviderOperation createSaveOperation (Object element , IDocument document , boolean overwrite )
32
68
throws CoreException {
33
- // TODO Auto-generated method stub
34
- return super .createSaveOperation (element , document , overwrite );
69
+ final DocumentProviderOperation delegate = super .createSaveOperation (element , document , overwrite );
70
+ return new DocumentProviderOperation () {
71
+ @ Override
72
+ protected void execute (IProgressMonitor monitor ) throws CoreException {
73
+ SubMonitor progress = SubMonitor .convert (monitor , 10 );
74
+
75
+ // Retrieve the file that is being saved
76
+ IFile file = getFile (element );
77
+ if (file == null ) {
78
+ return ;
79
+ }
80
+ IPreferenceStore preferenceStore = createProjectSpecificPreferenceStore (file .getProject ());
81
+ boolean runSaveActions = preferenceStore
82
+ .getBoolean (TypeScriptUIPreferenceConstants .EDITOR_SAVE_ACTIONS );
83
+
84
+ try {
85
+ delegate .run (progress .newChild (8 ));
86
+ if (runSaveActions ) {
87
+ try {
88
+ performSaveActions (file , document , progress .newChild (2 ), preferenceStore );
89
+ } catch (Exception e ) {
90
+ JSDTTypeScriptUIPlugin .log (e );
91
+ }
92
+ } else {
93
+ progress .setWorkRemaining (0 );
94
+ }
95
+ } catch (InterruptedException e ) {
96
+ Thread .currentThread ().interrupt ();
97
+ } catch (InvocationTargetException e ) {
98
+ throw new CoreException (
99
+ new Status (IStatus .ERROR , TypeScriptCorePlugin .PLUGIN_ID , "Error while saving " + file , e ));
100
+ }
101
+ }
102
+ };
103
+ }
104
+
105
+ private void performSaveActions (IFile file , IDocument document , IProgressMonitor monitor ,
106
+ IPreferenceStore preferenceStore ) {
107
+ boolean runFormat = preferenceStore .getBoolean (TypeScriptUIPreferenceConstants .EDITOR_SAVE_ACTIONS_FORMAT );
108
+ SubMonitor progress = SubMonitor .convert (monitor , (runFormat ? 10 : 0 ));
109
+ if (!runFormat ) {
110
+ return ;
111
+ }
112
+
113
+ IUndoManager manager = RefactoringCore .getUndoManager ();
114
+
115
+ CompositeChange saveActionsChange = new CompositeChange ("Save Actions" );
116
+ List <Change > undoChanges = new ArrayList <>();
117
+ boolean success = false ;
118
+ try {
119
+ manager .aboutToPerformChange (saveActionsChange );
120
+
121
+ // Format the file contents
122
+ if (runFormat ) {
123
+ TextFileChange change = new TextFileChange ("Format" , file );
124
+ try {
125
+ IIDETypeScriptProject tsProject = TypeScriptResourceUtil .getTypeScriptProject (file .getProject ());
126
+ final IIDETypeScriptFile tsFile = tsProject .openFile (file , document );
127
+ List <CodeEdit > codeEdits = tsFile .format (0 , document .getLength ()).get ();
128
+ change .setEdit (DocumentUtils .toTextEdit (codeEdits , document ));
129
+ change .initializeValidationData (new NullProgressMonitor ());
130
+ PerformChangeOperation performChangeOperation = new PerformChangeOperation (change );
131
+ ResourcesPlugin .getWorkspace ().run (performChangeOperation , progress .newChild (10 ));
132
+ Change undoChange = performChangeOperation .getUndoChange ();
133
+ if (undoChange != null ) {
134
+ undoChanges .add (undoChange );
135
+ }
136
+ } catch (Exception e ) {
137
+ JSDTTypeScriptUIPlugin .log (e );
138
+ }
139
+ }
140
+
141
+ success = true ;
142
+ } finally {
143
+ manager .changePerformed (saveActionsChange , success );
144
+ }
145
+
146
+ // Add an undo change if possible
147
+ if (!undoChanges .isEmpty ()) {
148
+ manager .addUndo (saveActionsChange .getName (), new CompositeChange (saveActionsChange .getName (),
149
+ undoChanges .toArray (new Change [undoChanges .size ()])));
150
+ }
151
+ }
152
+
153
+ private static IPreferenceStore createProjectSpecificPreferenceStore (IProject project ) {
154
+ List <IPreferenceStore > stores = new ArrayList <IPreferenceStore >();
155
+ if (project != null ) {
156
+ stores .add (new EclipsePreferencesAdapter (new ProjectScope (project ), TypeScriptUIPlugin .PLUGIN_ID ));
157
+ stores .add (new EclipsePreferencesAdapter (new ProjectScope (project ), TypeScriptCorePlugin .PLUGIN_ID ));
158
+ }
159
+ stores .add (new ScopedPreferenceStore (InstanceScope .INSTANCE , TypeScriptUIPlugin .PLUGIN_ID ));
160
+ stores .add (new ScopedPreferenceStore (InstanceScope .INSTANCE , TypeScriptCorePlugin .PLUGIN_ID ));
161
+ stores .add (new ScopedPreferenceStore (DefaultScope .INSTANCE , TypeScriptUIPlugin .PLUGIN_ID ));
162
+ stores .add (new ScopedPreferenceStore (DefaultScope .INSTANCE , TypeScriptCorePlugin .PLUGIN_ID ));
163
+ return new ChainedPreferenceStore (stores .toArray (new IPreferenceStore [stores .size ()]));
164
+ }
165
+
166
+ private IFile getFile (Object element ) {
167
+ return getFile (getFileInfo (element ));
168
+ }
169
+
170
+ private IFile getFile (FileInfo fileInfo ) {
171
+ if (fileInfo != null && fileInfo .fElement instanceof IFileEditorInput ) {
172
+ return ((IFileEditorInput ) fileInfo .fElement ).getFile ();
173
+ }
174
+ return null ;
35
175
}
36
176
}
0 commit comments