com.facebook.buck.intellij.plugin.actions.GoToBuckFile.java Source code

Java tutorial

Introduction

Here is the source code for com.facebook.buck.intellij.plugin.actions.GoToBuckFile.java

Source

/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.intellij.plugin.actions;

import com.facebook.buck.intellij.plugin.file.BuckFileUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;

/**
 * Go to its BUCK file for current source file.
 */
public class GoToBuckFile extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        final Project project = e.getProject();
        if (project == null) {
            return;
        }
        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (editor == null) {
            return;
        }
        final Document document = editor.getDocument();
        if (document == null) {
            return;
        }
        VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
        if (virtualFile == null) {
            return;
        }

        VirtualFile parent = virtualFile.getParent();
        if (parent == null) {
            return;
        }

        VirtualFile buckFile = parent.findChild(BuckFileUtil.getBuildFileName());
        while (buckFile == null && parent != null) {
            parent = parent.getParent();
            buckFile = parent.findChild(BuckFileUtil.getBuildFileName());
        }

        final VirtualFile file = buckFile;
        if (file != null) {
            ApplicationManager.getApplication().invokeLater(new Runnable() {
                @Override
                public void run() {
                    // This is for better cursor position.
                    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file);
                    Navigatable n = descriptor.setUseCurrentWindow(false);
                    if (!n.canNavigate()) {
                        return;
                    }
                    n.navigate(true);
                }
            }, ModalityState.NON_MODAL);
        }
    }
}