Skip to content

Commit

Permalink
Implement "Organize Imports"
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrey-easyesi committed Jul 11, 2018
1 parent 3423d04 commit 55ab608
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
7 changes: 7 additions & 0 deletions server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,13 @@ class Program {
return this.service.getEditsForRefactor(fileName, formatOptions, pos === end ? pos : { pos, end },
refactorName, actionName, void 0);
}
organizeImports(fileName: string, formatOptions: ts.FormatCodeSettings) {
if (! this.fileInProject(fileName)) return null;
if (! this.service.organizeImports) {
return "organizeImports requires TypeScript 2.9\nCurrent version: " + ts.version;
}
return this.service.organizeImports({ type: "file", fileName }, formatOptions, void 0);
}
}

var programCache: {[path: string]: Program};
Expand Down
1 change: 1 addition & 0 deletions src/netbeanstypescript/Bundle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
OpenIDE-Module-Display-Category=JavaScript
OpenIDE-Module-Name=TypeScript Editor
organize-imports=Organize Imports
60 changes: 60 additions & 0 deletions src/netbeanstypescript/OrganizeImportsAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package netbeanstypescript;

import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.text.JTextComponent;
import org.json.simple.JSONObject;
import org.netbeans.api.editor.EditorActionNames;
import org.netbeans.api.editor.EditorActionRegistration;
import org.netbeans.api.progress.ProgressUtils;
import org.netbeans.editor.BaseAction;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.Utilities;
import org.netbeans.modules.parsing.api.ParserManager;
import org.netbeans.modules.parsing.api.ResultIterator;
import org.netbeans.modules.parsing.api.Source;
import org.netbeans.modules.parsing.api.UserTask;
import org.netbeans.modules.parsing.spi.ParseException;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;

/**
*
* @author jeffrey
*/
@EditorActionRegistration(name=EditorActionNames.organizeImports, mimeType="text/typescript")
public class OrganizeImportsAction extends BaseAction {

@Override
public void actionPerformed(ActionEvent e, JTextComponent target) {
final BaseDocument doc = Utilities.getDocument(target);
if (doc == null) return;
final AtomicBoolean cancel = new AtomicBoolean();
class OrganizeImportsTask extends UserTask implements Runnable {
@Override
public void run() {
try {
ParserManager.parse(Collections.singleton(Source.create(doc)), this);
} catch (ParseException e) {
Exceptions.printStackTrace(e);
}
}
@Override
public void run(ResultIterator ri) throws ParseException {
FileObject fileObj = ri.getParserResult().getSnapshot().getSource().getFileObject();
try {
Object changes = TSService.callEx("organizeImports", fileObj,
TSFormatter.getFormattingSettings(doc));
if (changes == null || cancel.get()) return;
TSHintsProvider.doFixes(doc, fileObj, (List<JSONObject>) changes);
} catch (TSService.TSException e) {
e.notifyLater();
}
}
}
ProgressUtils.runOffEventDispatchThread(new OrganizeImportsTask(),
"Organize Imports", cancel, false);
}
}
11 changes: 6 additions & 5 deletions src/netbeanstypescript/TSHintsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.List;
import javax.swing.text.BadLocationException;
import org.json.simple.JSONObject;
import org.netbeans.editor.BaseDocument;
import org.netbeans.modules.csl.api.*;
import org.netbeans.modules.csl.api.Error;
import org.openide.DialogDisplayer;
Expand All @@ -59,7 +60,7 @@ public class TSHintsProvider implements HintsProvider {
@Override
public void computeHints(HintsManager manager, RuleContext context, List<Hint> hints) {}

private void doFixes(final RuleContext context, FileObject fileObj, final List<JSONObject> changes) {
public static void doFixes(final BaseDocument doc, FileObject fileObj, final List<JSONObject> changes) {
for (JSONObject change: changes) {
Object fileName = change.get("fileName");
if (! fileName.equals(fileObj.getPath())) {
Expand All @@ -69,12 +70,12 @@ private void doFixes(final RuleContext context, FileObject fileObj, final List<J
return;
}
}
context.doc.runAtomic(new Runnable() {
doc.runAtomic(new Runnable() {
@Override
public void run() {
try {
for (JSONObject change: changes) {
TSFormatter.applyEdits(context.doc, change.get("textChanges"));
TSFormatter.applyEdits(doc, change.get("textChanges"));
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
Expand Down Expand Up @@ -115,7 +116,7 @@ public String getDescription() {
}
@Override
public void implement() {
doFixes(context, fileObj, (List<JSONObject>) fix.get("changes"));
doFixes(context.doc, fileObj, (List<JSONObject>) fix.get("changes"));
}
@Override
public boolean isSafe() { return false; }
Expand Down Expand Up @@ -163,7 +164,7 @@ public void implement() {
new NotifyDescriptor.Message(error, NotifyDescriptor.ERROR_MESSAGE));
return;
}
doFixes(context, fileObj, (List<JSONObject>) edits.get("edits"));
doFixes(context.doc, fileObj, (List<JSONObject>) edits.get("edits"));
}
@Override
public boolean isSafe() { return false; }
Expand Down

0 comments on commit 55ab608

Please sign in to comment.