Example #1
0
  /**
   * Recreates the index for a given path, keeping the same read-write monitor. Returns the new
   * empty index or null if it didn't exist before. Warning: Does not check whether index is
   * consistent (not being used)
   */
  public synchronized Index recreateIndex(IPath containerPath) {
    // only called to over write an existing cached index...
    String containerPathString =
        containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
    try {
      // Path is already canonical
      IPath indexLocation = computeIndexLocation(containerPath);

      Index index = (Index) this.indexes.get(indexLocation);
      ReadWriteMonitor monitor = index == null ? null : index.monitor;

      if (VERBOSE)
        Util.verbose(
            "-> recreating index: "
                + indexLocation
                + " for path: "
                + containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
      index =
          new Index(indexLocation.toString(), containerPathString, false /* reuse index file */);
      this.indexes.put(indexLocation, index);
      index.monitor = monitor;
      return index;
    } catch (IOException e) {
      // The file could not be created. Possible reason: the project has been deleted.
      if (VERBOSE) {
        Util.verbose("-> failed to recreate index for path: " + containerPathString); // $NON-NLS-1$
        e.printStackTrace();
      }
      return null;
    }
  }
Example #2
0
 protected IStatus validateRubyScript(IResource resource) {
   ISourceFolderRoot root = getSourceFolderRoot();
   // root never null as validation is not done for working copies
   if (resource != null) {
     char[][] inclusionPatterns = ((SourceFolderRoot) root).fullInclusionPatternChars();
     char[][] exclusionPatterns = ((SourceFolderRoot) root).fullExclusionPatternChars();
     if (Util.isExcluded(resource, inclusionPatterns, exclusionPatterns))
       return new RubyModelStatus(IRubyModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH, this);
     if (!resource.isAccessible())
       return new RubyModelStatus(IRubyModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this);
   }
   if (name == null) {
     return new Status(
         IStatus.ERROR,
         RubyCore.PLUGIN_ID,
         -1,
         Messages.bind(Messages.convention_unit_nullName),
         null);
   }
   if (!org.rubypeople.rdt.internal.core.util.Util.isERBLikeFileName(name)) {
     return new Status(
         IStatus.ERROR,
         RubyCore.PLUGIN_ID,
         -1,
         Messages.bind(Messages.convention_unit_notERBName),
         null);
   }
   IStatus status = ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE);
   if (!status.isOK()) {
     return status;
   }
   return RubyModelStatus.VERIFIED_OK;
 }
Example #3
0
 public void warn(ID id, ISourcePosition position, String message, Object... data) {
   if (Util.ignore(message)) {
     return;
   }
   if (message.equals(
       "Statement not reached.")) { // TODO Categorize problems that JRuby provides in one place
     String value = RubyCore.getOption(RubyCore.COMPILER_PB_UNREACHABLE_CODE);
     if (value == null || value.equals(RubyCore.WARNING)) {
       warnings.add(new Warning(position, message));
     }
     if (value != null && value.equals(RubyCore.ERROR)) {
       warnings.add(new Error(position, message));
     }
     return;
   } else if (message.equals("parenthesize argument(s) for future version")) {
     ISourcePosition pos =
         new IDESourcePosition(
             position.getFile(),
             position.getStartLine(),
             position.getEndLine(),
             position.getStartOffset(),
             position.getEndOffset() - 2);
     warnings.add(new Warning(pos, message, IProblem.ParenthesizeArguments));
     return;
   }
   warnings.add(new Warning(position, message));
 }
Example #4
0
 private void writeSavedIndexNamesFile() {
   BufferedWriter writer = null;
   try {
     writer = new BufferedWriter(new FileWriter(savedIndexNamesFile));
     writer.write(DiskIndex.SIGNATURE);
     writer.write('+');
     writer.write(getRubyPluginWorkingLocation().toOSString());
     writer.write('\n');
     Object[] keys = indexStates.keyTable;
     Object[] states = indexStates.valueTable;
     for (int i = 0, l = states.length; i < l; i++) {
       IPath key = (IPath) keys[i];
       if (key != null && !key.isEmpty() && states[i] == SAVED_STATE) {
         writer.write(key.lastSegment());
         writer.write('\n');
       }
     }
   } catch (IOException ignored) {
     if (VERBOSE)
       Util.verbose("Failed to write saved index file names", System.err); // $NON-NLS-1$
   } finally {
     if (writer != null) {
       try {
         writer.close();
       } catch (IOException e) {
         // ignore
       }
     }
   }
 }
Example #5
0
  private synchronized void updateIndexState(IPath indexLocation, Integer indexState) {
    if (indexLocation.isEmpty()) throw new IllegalArgumentException();

    getIndexStates(); // ensure the states are initialized
    if (indexState != null) {
      if (indexState.equals(indexStates.get(indexLocation))) return; // not changed
      indexStates.put(indexLocation, indexState);
    } else {
      if (!indexStates.containsKey(indexLocation)) return; // did not exist anyway
      indexStates.removeKey(indexLocation);
    }

    writeSavedIndexNamesFile();

    if (VERBOSE) {
      String state = "?"; // $NON-NLS-1$
      if (indexState == SAVED_STATE) state = "SAVED"; // $NON-NLS-1$
      else if (indexState == UPDATING_STATE) state = "UPDATING"; // $NON-NLS-1$
      else if (indexState == UNKNOWN_STATE) state = "UNKNOWN"; // $NON-NLS-1$
      else if (indexState == REBUILDING_STATE) state = "REBUILDING"; // $NON-NLS-1$
      Util.verbose(
          "-> index state updated to: "
              + state
              + " for: "
              + indexLocation); //$NON-NLS-1$ //$NON-NLS-2$
    }
  }
Example #6
0
  private void deleteIndexFiles(SimpleSet pathsToKeep) {
    File[] indexesFiles = getSavedIndexesDirectory().listFiles();
    if (indexesFiles == null) return;

    for (int i = 0, l = indexesFiles.length; i < l; i++) {
      String fileName = indexesFiles[i].getAbsolutePath();
      if (pathsToKeep != null && pathsToKeep.includes(fileName)) continue;
      String suffix = ".index"; // $NON-NLS-1$
      if (fileName.regionMatches(
          true, fileName.length() - suffix.length(), suffix, 0, suffix.length())) {
        if (VERBOSE) Util.verbose("Deleting index file " + indexesFiles[i]); // $NON-NLS-1$
        indexesFiles[i].delete();
      }
    }
  }
Example #7
0
  private synchronized void removeIndexesState(IPath[] locations) {
    getIndexStates(); // ensure the states are initialized
    int length = locations.length;
    boolean changed = false;
    for (int i = 0; i < length; i++) {
      if (locations[i] == null) continue;
      if ((indexStates.removeKey(locations[i]) != null)) {
        changed = true;
        if (VERBOSE) {
          Util.verbose("-> index state updated to: ? for: " + locations[i]); // $NON-NLS-1$
        }
      }
    }
    if (!changed) return;

    writeSavedIndexNamesFile();
  }
Example #8
0
 /** Removes the index for a given path. This is a no-op if the index did not exist. */
 public synchronized void removeIndex(IPath containerPath) {
   if (VERBOSE) Util.verbose("removing index " + containerPath); // $NON-NLS-1$
   IPath indexLocation = computeIndexLocation(containerPath);
   Index index = getIndex(indexLocation);
   File indexFile = null;
   if (index != null) {
     index.monitor = null;
     indexFile = index.getIndexFile();
   }
   if (indexFile == null)
     indexFile =
         new File(
             indexLocation.toOSString()); // index is not cached yet, but still want to delete the
   // file
   if (indexFile.exists()) indexFile.delete();
   this.indexes.remove(indexLocation);
   updateIndexState(indexLocation, null);
 }
Example #9
0
 private char[][] readIndexState(String dirOSString) {
   try {
     char[] savedIndexNames =
         org.rubypeople.rdt.core.util.Util.getFileCharContent(savedIndexNamesFile, null);
     if (savedIndexNames.length > 0) {
       char[][] names = CharOperation.splitOn('\n', savedIndexNames);
       if (names.length > 1) {
         // First line is DiskIndex signature + saved plugin working location (see
         // writeSavedIndexNamesFile())
         String savedSignature = DiskIndex.SIGNATURE + "+" + dirOSString; // $NON-NLS-1$
         if (savedSignature.equals(new String(names[0]))) return names;
       }
     }
   } catch (IOException ignored) {
     if (VERBOSE) Util.verbose("Failed to read saved index file names"); // $NON-NLS-1$
   }
   return null;
 }
Example #10
0
 public IPath computeIndexLocation(IPath containerPath) {
   IPath indexLocation = (IPath) this.indexLocations.get(containerPath);
   if (indexLocation == null) {
     String pathString = containerPath.toOSString();
     checksumCalculator.reset();
     checksumCalculator.update(pathString.getBytes());
     String fileName = Long.toString(checksumCalculator.getValue()) + ".index"; // $NON-NLS-1$
     if (VERBOSE)
       Util.verbose(
           "-> index name for " + pathString + " is " + fileName); // $NON-NLS-1$ //$NON-NLS-2$
     // to share the indexLocation between the indexLocations and indexStates tables, get the key
     // from the
     // indexStates table
     indexLocation =
         (IPath) getIndexStates().getKey(getRubyPluginWorkingLocation().append(fileName));
     this.indexLocations.put(containerPath, indexLocation);
   }
   return indexLocation;
 }
Example #11
0
 public void saveIndex(Index index) throws IOException {
   // must have permission to write from the write monitor
   if (index.hasChanged()) {
     if (VERBOSE) Util.verbose("-> saving index " + index.getIndexFile()); // $NON-NLS-1$
     index.save();
   }
   synchronized (this) {
     IPath containerPath = new Path(index.containerPath);
     if (this.jobEnd > this.jobStart) {
       for (int i = this.jobEnd; i > this.jobStart; i--) { // skip the current job
         IJob job = this.awaitingJobs[i];
         if (job instanceof IndexRequest)
           if (((IndexRequest) job).containerPath.equals(containerPath)) return;
       }
     }
     IPath indexLocation = computeIndexLocation(containerPath);
     updateIndexState(indexLocation, SAVED_STATE);
   }
 }
Example #12
0
  private void rebuildIndex(IPath indexLocation, IPath containerPath) {
    Object target = RubyModel.getTarget(containerPath, true);
    if (target == null) return;

    if (VERBOSE)
      Util.verbose(
          "-> request to rebuild index: "
              + indexLocation
              + " path: "
              + containerPath); //$NON-NLS-1$ //$NON-NLS-2$

    updateIndexState(indexLocation, REBUILDING_STATE);
    IndexRequest request = null;
    if (target instanceof IProject) {
      IProject p = (IProject) target;
      if (RubyProject.hasRubyNature(p)) request = new IndexAllProject(p, this);
    } else if (target instanceof File) {
      request = new AddExternalFolderToIndex(containerPath, this);
    }
    if (request != null) request(request);
  }
Example #13
0
  private SimpleLookupTable getIndexStates() {
    if (this.indexStates != null) return this.indexStates;

    this.indexStates = new SimpleLookupTable();
    IPath indexesDirectoryPath = getRubyPluginWorkingLocation();
    char[][] savedNames = readIndexState(indexesDirectoryPath.toOSString());
    if (savedNames != null) {
      for (int i = 1, l = savedNames.length;
          i < l;
          i++) { // first name is saved signature, see readIndexState()
        char[] savedName = savedNames[i];
        if (savedName.length > 0) {
          IPath indexLocation = indexesDirectoryPath.append(new String(savedName)); // shares
          // indexesDirectoryPath
          // 's segments
          if (VERBOSE) Util.verbose("Reading saved index file " + indexLocation); // $NON-NLS-1$
          this.indexStates.put(indexLocation, SAVED_STATE);
        }
      }
    } else {
      deleteIndexFiles();
    }
    return this.indexStates;
  }
Example #14
0
 private String replaceNonRubyCodeWithWhitespace(String source) {
   return new String(Util.replaceNonRubyCodeWithWhitespace(source));
 }
Example #15
0
 @Override
 protected char[] getCharacters(IProgressMonitor pm, RubyScriptElementInfo unitInfo)
     throws RubyModelException {
   char[] cs = super.getCharacters(pm, unitInfo);
   return Util.replaceNonRubyCodeWithWhitespace(new String(cs));
 }
Example #16
0
  /**
   * Returns the index for a given project, according to the following algorithm: - if index is
   * already in memory: answers this one back - if (reuseExistingFile) then read it and return this
   * index and record it in memory - if (createIfMissing) then create a new empty index and record
   * it in memory Warning: Does not check whether index is consistent (not being used)
   */
  public synchronized Index getIndex(
      IPath containerPath,
      IPath indexLocation,
      boolean reuseExistingFile,
      boolean createIfMissing) {
    // Path is already canonical per construction
    Index index = getIndex(indexLocation);
    if (index == null) {
      Object state = getIndexStates().get(indexLocation);
      Integer currentIndexState = state == null ? UNKNOWN_STATE : (Integer) state;
      if (currentIndexState == UNKNOWN_STATE) {
        // should only be reachable for query jobs
        // IF you put an index in the cache, then AddJarFileToIndex fails because it thinks there is
        // nothing to
        // do
        rebuildIndex(indexLocation, containerPath);
        return null;
      }

      // index isn't cached, consider reusing an existing index file
      String containerPathString =
          containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
      String indexLocationString = indexLocation.toOSString();
      if (reuseExistingFile) {
        File indexFile = new File(indexLocationString);
        if (indexFile
            .exists()) { // check before creating index so as to avoid creating a new empty index if
                         // file is missing
          try {
            index =
                new Index(indexLocationString, containerPathString, true /* reuse index file */);
            this.indexes.put(indexLocation, index);
            return index;
          } catch (IOException e) {
            // failed to read the existing file or its no longer compatible
            if (currentIndexState
                != REBUILDING_STATE) { // rebuild index if existing file is corrupt, unless the
                                       // index is already being rebuilt
              if (VERBOSE)
                Util.verbose(
                    "-> cannot reuse existing index: "
                        + indexLocationString
                        + " path: "
                        + containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
              rebuildIndex(indexLocation, containerPath);
              return null;
            }
            /* index = null; */
            // will fall thru to createIfMissing & create a empty index for the rebuild
            // all job to populate
          }
        }
        if (currentIndexState == SAVED_STATE) { // rebuild index if existing file is missing
          rebuildIndex(indexLocation, containerPath);
          return null;
        }
      }
      // index wasn't found on disk, consider creating an empty new one
      if (createIfMissing) {
        try {
          if (VERBOSE)
            Util.verbose(
                "-> create empty index: "
                    + indexLocationString
                    + " path: "
                    + containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
          index =
              new Index(
                  indexLocationString, containerPathString, false /* do not reuse index file */);
          this.indexes.put(indexLocation, index);
          return index;
        } catch (IOException e) {
          if (VERBOSE)
            Util.verbose(
                "-> unable to create empty index: "
                    + indexLocationString
                    + " path: "
                    + containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
          // The file could not be created. Possible reason: the project has been deleted.
          return null;
        }
      }
    }
    // System.out.println(" index name: " + path.toOSString() + " <----> " +
    // index.getIndexFile().getName());
    return index;
  }