PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/structure/SpecToggler.java

#
Java | 141 lines | 106 code | 16 blank | 19 comment | 28 complexity | df59c9063bfd78748dcbbbba7cdae973 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * RubyActions.java - Actions for Ruby plugin
  3. *
  4. * Copyright 2008 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby.structure;
  21. import org.gjt.sp.jedit.Buffer;
  22. import org.gjt.sp.jedit.View;
  23. import org.gjt.sp.jedit.jEdit;
  24. import java.io.File;
  25. import java.io.FilenameFilter;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. public class SpecToggler {
  29. public static final Map<File, File> SPEC_TO_CODE = new HashMap<File, File>();
  30. public static final Map<File, File> CODE_TO_SPEC = new HashMap<File, File>();
  31. private File currentFile;
  32. private View view;
  33. private String bufferDirectory;
  34. private String bufferName;
  35. public SpecToggler(View theView) {
  36. view = theView;
  37. Buffer buffer = view.getBuffer();
  38. bufferDirectory = buffer.getDirectory();
  39. bufferName = buffer.getName();
  40. currentFile = new File(bufferDirectory, buffer.getName());
  41. }
  42. public void toggleSpec() {
  43. if (isBufferSpecFile()) {
  44. toggleToCode();
  45. } else {
  46. toggleToSpec();
  47. }
  48. }
  49. private void toggleToCode() {
  50. if (SPEC_TO_CODE.containsKey(currentFile)) {
  51. openFile(SPEC_TO_CODE.get(currentFile));
  52. } else {
  53. String[] parts = bufferDirectory.split(File.separatorChar + "spec" + File.separatorChar);
  54. File baseDir = new File(parts[0]);
  55. String subPath = parts[1];
  56. String codeFileName = bufferName.replaceFirst("_spec\\.rb", ".rb");
  57. File code = tryToOpenFile(baseDir, subPath, codeFileName);
  58. if (code == null) {
  59. subPath = "app" + File.separatorChar + subPath;
  60. code = tryToOpenFile(baseDir, subPath, codeFileName);
  61. if (code == null) {
  62. code = tryToOpenFile(baseDir, subPath, bufferName.replaceFirst("_spec\\.rb", ""));
  63. }
  64. }
  65. if (code != null) {
  66. SPEC_TO_CODE.put(currentFile, code);
  67. CODE_TO_SPEC.put(code, currentFile);
  68. }
  69. }
  70. }
  71. private void toggleToSpec() {
  72. if (CODE_TO_SPEC.containsKey(currentFile)) {
  73. openFile(CODE_TO_SPEC.get(currentFile));
  74. } else {
  75. File specDirectory = getSpecDirectory();
  76. if (specDirectory != null) {
  77. String subPath = bufferDirectory.replaceFirst(specDirectory.getParent(), "");
  78. String specFileName = bufferName.replaceFirst("\\.rb$","_spec.rb");
  79. File spec = tryToOpenFile(specDirectory, subPath, specFileName);
  80. if (spec == null) {
  81. subPath = subPath.replaceFirst("^/app","");
  82. spec = tryToOpenFile(specDirectory, subPath, specFileName);
  83. if (spec == null) {
  84. spec = tryToOpenFile(specDirectory, subPath, bufferName + "_spec.rb");
  85. }
  86. }
  87. if (spec != null) {
  88. SPEC_TO_CODE.put(spec, currentFile);
  89. CODE_TO_SPEC.put(currentFile, spec);
  90. }
  91. }
  92. }
  93. }
  94. private File tryToOpenFile(File directory, String subPath, String fileName) {
  95. File file = new File(new File(directory.getPath(), subPath), fileName);
  96. if (file.exists() && file.isFile()) {
  97. openFile(file);
  98. return file;
  99. } else {
  100. return null;
  101. }
  102. }
  103. private void openFile(File file) {
  104. jEdit.openFile(view, file.getAbsolutePath());
  105. }
  106. private File getSpecDirectory() {
  107. File parent = new File(bufferDirectory);
  108. File specDir = null;
  109. while ((parent = parent.getParentFile()) != null) {
  110. String[] files = parent.list(new FilenameFilter() {
  111. public boolean accept(File dir, String name) {
  112. return name.equals("spec");
  113. }
  114. });
  115. if (files != null && files.length > 0) {
  116. specDir = new File(parent.getPath(), files[0]);
  117. break;
  118. }
  119. }
  120. return specDir;
  121. }
  122. private boolean isBufferSpecFile() {
  123. return bufferName.contains("_spec");
  124. }
  125. }