/*
 * Copyright 2016 The Bazel Authors. All rights reserved.
 *
 * 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.google.idea.blaze.base.projectview.section.sections;

import com.google.idea.blaze.base.io.FileOperationProvider;
import com.google.idea.blaze.base.projectview.parser.ParseContext;
import com.google.idea.blaze.base.projectview.parser.ProjectViewParser;
import com.google.idea.blaze.base.projectview.section.ScalarSection;
import com.google.idea.blaze.base.projectview.section.ScalarSectionParser;
import com.google.idea.blaze.base.projectview.section.SectionKey;
import com.google.idea.blaze.base.projectview.section.SectionParser;
import java.io.File;
import javax.annotation.Nullable;

/** Section for a project-specific bazel binary override. */
public class BazelBinarySection {
  public static final SectionKey<File, ScalarSection<File>> KEY = SectionKey.of("bazel_binary");
  public static final SectionParser PARSER = new BazelBinarySectionParser();

  private static class BazelBinarySectionParser extends ScalarSectionParser<File> {
    BazelBinarySectionParser() {
      super(KEY, ':');
    }

    @Override
    @Nullable
    protected File parseItem(ProjectViewParser parser, ParseContext parseContext, String text) {
      File file = new File(text);
      if (text.isEmpty() || !file.isAbsolute()) {
        parseContext.addError(errorMessage(text, "Specify an absolute file path"));
        return null;
      }
      if (!FileOperationProvider.getInstance().isFile(file)) {
        parseContext.addError(errorMessage(text, "Specified file doesn't exist"));
        return null;
      }
      return file;
    }

    private static String errorMessage(String filePath, String error) {
      return String.format("Invalid bazel binary location: %s\n%s", filePath, error);
    }

    @Override
    protected void printItem(StringBuilder sb, File item) {
      sb.append(item.getPath());
    }

    @Override
    public ItemType getItemType() {
      return ItemType.Other;
    }

    @Override
    public String quickDocs() {
      return "A project-specific override for the bazel binary location.";
    }
  }
}

@@ -1767,189 +1767,451 @@
     
-File file = new File(text);%0A      if (text.isEmpty() %7C%7C !file.isAbsolute()) %7B%0A        parseContext.addError(errorMessage(text, %22Specify an absolute file path%22));%0A        return null
+if (text.isEmpty()) %7B%0A        parseContext.addError(errorMessage(text, %22Specify a file path%22));%0A        return null;%0A      %7D%0A      File file = new File(text);%0A      if (!file.isAbsolute()) %7B%0A        // If it's relative, the path will be rooted in the IntelliJ application directory and%0A        // not the workspace root. So, relativize the path to workspace root here.%0A        file = parseContext.getWorkspacePathResolver().resolveToFile(text)
 ;%0A  

Support relative paths for bazel_binary in .bazelproject (bazelbuild/intellij PR import #970)