Create Dart class

Hi!

How can I create a Dart class and methods? I know that for Java I use the JavaPsiFacade.getElementFactory(project) but what about Dart?

0

Hi Lung,

You may use 

PsiFileFactory.getInstance(project).createFileFromText(...)

and then find whatever PSI you need in the created PsiFile. But I'm not sure that's what you actually need. What are you trying to accomplish and why do you think you need an artificial PSI?

Alex

0

Thanks so much for your reply! I'm trying to add some methods to a dart class. My base class is like this:

class S {
Locale locale;

S(this.locale);

static S of(BuildContext context) => Localizations.of<S>(context, S);
static final GeneratedLocalizationsDelegate delegate = new GeneratedLocalizationsDelegate();

String get backButtonTooltip => localizations[locale.languageCode]["backButtonTooltip"];
}

And I want to add more getters like this
String get name => localizations[locale.languageCode]["name"];
String get back => localizations[locale.languageCode]["back"];

 

0

First of all, you seem to be writing a plugin :). Just curious what kind of plugin is it?

You may try an alternative way to modify file content, instead of PSI modification you may modify `Document` text. Just take an instance of Document and call `insertString()`.

More about PSI, Document, etc. here: https://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview.html

0

I'm writing and plugin for Flutter i18n. Thanks again for your help!

0

Another question. Once I've build the class and saved it to the file as you helped me, how can I format the file to standard Dart?

0

> I'm writing and plugin for Flutter i18n.

Interesting! Is it open source / publicly available? Are you working for Google?

To reformat the whole file using the canonical dartfmt tool (via Dart Analysis Server) you may use 

com.jetbrains.lang.dart.ide.actions.DartStyleAction.runDartfmt()
0

Thanks again for your reply. Your are so helpful. I'll make it open source this week. I just have to as some actions and I'm done. I'll update here when it's ready.

0

Another question. I'm using this to listen when the file changes but I get the instance of the file just after the change is commited. Any suggestions?

valuesFolder.children.forEach {
documentManager.getDocument(psiManager.findFile(it)!!)!!.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent?) {
//generate my files
}
})
}
0

I'm afraid I do not get the question.

0

The documentChanged is called just BEFORE the document is commited. When I generate my files they are using the document just BEFORE the edit.

0

Feel free to PR if you have the time. Thanks again for your help.

https://github.com/long1eu/flutter_i18n

0

Hi!

I'm trying to update only the part that changed in the arb file. After I generate the new class and retrieve the old one, I'm trying to make the change like this:

runWriteAction {
dartDocument.setReadOnly(false)
originalDartClass.replace(newDartClass)
documentManager.doPostponedOperationsAndUnblockDocument(dartDocument)
dartDocument.setReadOnly(true)
}

But I'm getting this:

2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - IntelliJ IDEA 2017.2.5 Build #IU-172.4343.14 
2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - JDK: 1.8.0_144
2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - VM: Java HotSpot(TM) 64-Bit Server VM
2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - Vendor: Oracle Corporation
2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - OS: Linux
2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - Last Action: EditorBackSpace
2017-10-28 23:07:19,464 [ 18208] ERROR - enapi.editor.impl.DocumentImpl - Current Command: Backspace
2017-10-28 23:07:19,885 [ 18629] ERROR - enapi.editor.impl.DocumentImpl - Document is locked by write PSI operations. Use PsiDocumentManager.doPostponedOperationsAndUnblockDocument() to commit PSI changes to the document.
Unprocessed elements: CLASS_DEFINITION(2318,4328)
java.lang.RuntimeException: Document is locked by write PSI operations. Use PsiDocumentManager.doPostponedOperationsAndUnblockDocument() to commit PSI changes to the document.
Unprocessed elements: CLASS_DEFINITION(2318,4328)

 

0

UPDATE on my last post. I was creating the new class using the PsiFactoryFactory and extracted the class from there. That file was not committed. So I changed to the string replace way.

0

请先登录再写评论。