ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(yamlFile.getProject());
final Document document = manager.getDocument(yamlFile);
if (document != null) {
manager.commitDocument(document);
}
final PsiElement yamlDocu = PsiTreeUtil.findChildOfType(yamlFile, YAMLDocument.class);
if(yamlDocu == null) {
return;
}
final YamlKeyFinder.MatchedKey goToPsi = YamlKeyFinder.findLastValueElement(yamlDocu, keyName);
if(goToPsi == null) {
return;
}
// search indent and EOL value
String indent = findIndent(goToPsi.getYamlKeyValue());
String eol = findEol(goToPsi.getYamlKeyValue());
String currentIndentOffset = "";
PsiElement lastKnownPsiElement = goToPsi.getYamlKeyValue();
if(lastKnownPsiElement instanceof YAMLKeyValue) {
currentIndentOffset = ((YAMLKeyValue) lastKnownPsiElement).getValueIndent();
}
String insertString = "";
String[] missingKeys = goToPsi.getMissingKeys();
for ( int i = 0; i < missingKeys.length; i++ ) {
String currentKeyName = missingKeys[i];
// add indent
insertString += eol + currentIndentOffset + StringUtils.repeat(indent, i);
// on last key name we dont need new lines
if(goToPsi.getMissingKeys().length - 1 == i) {
// we are developer should other translate it
insertString += currentKeyName + ": '" + translation + "'";
} else {
insertString += currentKeyName + ":";
}
}
// @TODO: check is last array line on contains eol and indent and move above this line
final String finalInsertString = insertString;
new WriteCommandAction(yamlFile.getProject()) {
@Override
protected void run(Result result) throws Throwable {
document.insertString(goToPsi.getYamlKeyValue().getTextRange().getEndOffset(), finalInsertString);
manager.doPostponedOperationsAndUnblockDocument(document);
manager.commitDocument(document);
}
@Override
public String getGroupID() {
return "Translation Extraction";
}
}.execute();
if(!openFile) {
return;
}
// navigate to new psi element
// @TODO: jump into quote value
manager.commitAndRunReadAction(new Runnable() {
@Override
public void run() {
YAMLKeyValue psiElement = YamlKeyFinder.find(yamlDocu, keyName);
if(psiElement != null) {
Symfony2SearchForm.navigateToPsiElement(psiElement.getValue());