Search in sources :

Example 1 with BuckFile

use of com.facebook.buck.intellij.ideabuck.lang.BuckFile in project buck by facebook.

the class BuckBuildUtil method extractBuckTarget.

/**
   * Get the buck target from a buck file.
   * TODO(#7908675): We should use Buck's own classes for it.
   */
public static String extractBuckTarget(Project project, VirtualFile file) {
    BuckFile buckFile = (BuckFile) PsiManager.getInstance(project).findFile(file);
    if (buckFile == null) {
        return null;
    }
    PsiElement[] children = buckFile.getChildren();
    for (PsiElement child : children) {
        if (child.getNode().getElementType() == BuckTypes.RULE_BLOCK) {
            PsiElement ruleName = child.getFirstChild();
            // Find rule "project_config"
            if (ruleName != null && BuckPsiUtils.testType(ruleName, BuckTypes.RULE_NAME) && ruleName.getText().equals(PROJECT_CONFIG_RULE_NAME)) {
                // Find property "src_target"
                PsiElement bodyElement = BuckPsiUtils.findChildWithType(child, BuckTypes.RULE_BODY);
                return getPropertyValue((BuckRuleBody) bodyElement, SRC_TARGET_PROPERTY_NAME);
            }
        }
    }
    return null;
}
Also used : BuckFile(com.facebook.buck.intellij.ideabuck.lang.BuckFile) PsiElement(com.intellij.psi.PsiElement)

Example 2 with BuckFile

use of com.facebook.buck.intellij.ideabuck.lang.BuckFile in project buck by facebook.

the class BuckCopyPasteProcessor method preprocessOnPaste.

@Override
public String preprocessOnPaste(Project project, PsiFile psiFile, Editor editor, String text, RawText rawText) {
    if (!(psiFile instanceof BuckFile)) {
        return text;
    }
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final SelectionModel selectionModel = editor.getSelectionModel();
    // Pastes in block selection mode (column mode) are not handled by a CopyPasteProcessor.
    final int selectionStart = selectionModel.getSelectionStart();
    final PsiElement element = psiFile.findElementAt(selectionStart);
    if (element == null) {
        return text;
    }
    if (BuckPsiUtils.hasElementType(element.getNode(), TokenType.WHITE_SPACE, BuckTypes.SINGLE_QUOTED_STRING, BuckTypes.DOUBLE_QUOTED_STRING)) {
        PsiElement property = BuckPsiUtils.findAncestorWithType(element, BuckTypes.PROPERTY);
        if (checkPropertyName(property)) {
            return formatPasteText(text, element, project);
        }
    }
    return text;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) BuckFile(com.facebook.buck.intellij.ideabuck.lang.BuckFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Aggregations

BuckFile (com.facebook.buck.intellij.ideabuck.lang.BuckFile)2 PsiElement (com.intellij.psi.PsiElement)2 Document (com.intellij.openapi.editor.Document)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1