/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * 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.intellij.ide.macro;

import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.vfs.VirtualFile;

public class FileDirPathFromParentMacro extends Macro {
  @Override
  public String getName() {
    return "FileDirPathFromParent";
  }

  @Override
  public String getDescription() {
    return IdeBundle.message("macro.file.directory.from.parent");
  }

  @Override
  public String expand(DataContext dataContext) {
    return null;
  }

  @Override
  public String expand(DataContext dataContext, String... args) throws ExecutionCancelledException {
    if(args.length == 0) {
      return super.expand(dataContext, args);
    }
    String param = args[0];
    VirtualFile vFile = getVirtualDirOrParent(dataContext);
    StringBuilder result = new StringBuilder();
    while (vFile != null && !param.equalsIgnoreCase(vFile.getName())) {
      result.insert(0, vFile.getName() + "/");
      vFile = vFile.getParent();
    }
    return result.toString();
  }
}

@@ -749,23 +749,206 @@
 api.
-vfs.VirtualFile
+util.SystemInfo;%0Aimport com.intellij.openapi.util.io.FileUtil;%0Aimport com.intellij.openapi.util.text.StringUtil;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport org.jetbrains.annotations.NotNull
 ;%0A%0Ap
@@ -1002,16 +1002,70 @@
 Macro %7B%0A
+%0A  private static final String PATH_DELIMITER = %22/%22;%0A%0A
   @Overr
@@ -1539,181 +1539,1173 @@
     
-String param = 
+VirtualFile dir = getVirtualDirOrParent(dataContext);%0A    if (dir == null) %7B%0A      return %22%22;%0A    %7D%0A    String dirPath = dir.getPath();%0A    String surroundedSubDir = surroundWithSlashes(FileUtil.toSystemIndependentName(
 args%5B0%5D
+))
 ;%0A    
-VirtualFile vFile = getVirtualDirOrParent(dataContext);%0A    StringBuilder result = new StringBuilder();%0A    while (vFile != null && !param.e
+String surroundedDirPath = surroundWithSlashes(dirPath);%0A    if (surroundedSubDir.length() == 1) %7B%0A      return surroundedDirPath.substring(1);%0A    %7D%0A    int ind = lastIndexOf(surroundedDirPath,%0A                          surroundedSubDir,%0A                          surroundedDirPath.length(),%0A                          !SystemInfo.isFileSystemCaseSensitive);%0A    if (ind %3E= 0) %7B%0A      return surroundedDirPath.substring(ind + surroundedSubDir.length());%0A    %7D%0A    return dirPath;%0A  %7D%0A%0A  private static int lastIndexOf(@NotNull CharSequence buffer, @NotNull String pattern, int maxIndex, boolean ignoreCase) %7B%0A    int patternLength = pattern.length();%0A    int end = buffer.length() - patternLength;%0A    if (maxIndex %3E end) %7B%0A      maxIndex = end;%0A    %7D%0A    for (int i = maxIndex; i %3E= 0; i--) %7B%0A      boolean found = true;%0A      for (int j = 0; j %3C patternLength; j++) %7B%0A        if (ignoreCase) %7B%0A          if (StringUtil.charsE
 qual
-s
 Igno
@@ -2715,142 +2715,808 @@
 ase(
-vFile.getName())) %7B%0A      result.insert(0, vFile.getName() + %22/%22);%0A      vFile = vFile.getParent();%0A    %7D%0A    return result.toString()
+pattern.charAt(j), buffer.charAt(i + j))) %7B%0A            found = false;%0A            break;%0A          %7D%0A        %7D%0A        else %7B%0A          if (pattern.charAt(j) != buffer.charAt(i + j)) %7B%0A            found = false;%0A            break;%0A          %7D%0A        %7D%0A      %7D%0A      if (found) %7B%0A        return i;%0A      %7D%0A    %7D%0A    return -1;%0A  %7D%0A%0A  @NotNull%0A  private static String surroundWithSlashes(@NotNull String path) %7B%0A    if (path.isEmpty()) %7B%0A      return PATH_DELIMITER;%0A    %7D%0A    boolean prepend = !path.startsWith(PATH_DELIMITER);%0A    boolean append = !path.endsWith(PATH_DELIMITER);%0A    if (prepend && append) %7B%0A      return PATH_DELIMITER + path + PATH_DELIMITER;%0A    %7D%0A    if (prepend) %7B%0A      path = PATH_DELIMITER + path;%0A    %7D%0A    if (append) %7B%0A      path += PATH_DELIMITER;%0A    %7D%0A    return path
 ;%0A  

support passing compound relative path to $FileDirPathFromParent(...)$ macro (fixes WEB-18982)