/**
  * Creates a compilation unit.
  *
  * @exception ModelException if unable to create the compilation unit.
  */
 protected void executeOperation() throws ModelException {
   try {
     // beginTask(Messages.operation_createUnitProgress, 2);
     ModelElementDelta delta = newModelElementDelta();
     ISourceModule unit = getSourceModule();
     IScriptFolder pkg = (IScriptFolder) getParentElement();
     IContainer folder = (IContainer) pkg.getResource();
     worked(1);
     IFile compilationUnitFile = folder.getFile(new Path(fName));
     if (compilationUnitFile.exists()) {
       // update the contents of the existing unit if fForce is true
       if (force) {
         IBuffer buffer = unit.getBuffer();
         if (buffer == null) return;
         buffer.setContents(fSource);
         unit.save(new NullProgressMonitor(), false);
         resultElements = new IModelElement[] {unit};
         if (!Util.isExcluded(unit) && unit.getParent().exists()) {
           for (int i = 0; i < resultElements.length; i++) {
             delta.changed(resultElements[i], IModelElementDelta.F_CONTENT);
           }
           addDelta(delta);
         }
       } else {
         throw new ModelException(
             new ModelStatus(
                 IModelStatusConstants.NAME_COLLISION,
                 Messages.bind(
                     Messages.status_nameCollision,
                     compilationUnitFile.getFullPath().toString())));
       }
     } else {
       try {
         String encoding = null;
         try {
           encoding = folder.getDefaultCharset(); // get folder encoding as file is not accessible
         } catch (CoreException ce) {
           // use no encoding
         }
         InputStream stream =
             new ByteArrayInputStream(
                 encoding == null ? fSource.getBytes() : fSource.getBytes(encoding));
         createFile(folder, unit.getElementName(), stream, force);
         resultElements = new IModelElement[] {unit};
         if (!Util.isExcluded(unit) && unit.getParent().exists()) {
           for (int i = 0; i < resultElements.length; i++) {
             delta.added(resultElements[i]);
           }
           addDelta(delta);
         }
       } catch (IOException e) {
         throw new ModelException(e, IModelStatusConstants.IO_EXCEPTION);
       }
     }
     worked(1);
   } finally {
     done();
   }
 }
示例#2
0
 /** Removes a buffer from the table of open buffers. */
 protected void removeBuffer(IBuffer buffer) {
   if (VERBOSE) {
     String owner = ((Openable) buffer.getOwner()).toStringWithAncestors();
     System.out.println("Removing buffer for " + owner); // $NON-NLS-1$
   }
   this.openBuffers.remove(buffer.getOwner());
   if (VERBOSE) {
     System.out.println(
         "-> Buffer cache filling ratio = "
             + NumberFormat.getInstance().format(this.openBuffers.fillingRatio())
             + "%"); //$NON-NLS-1$//$NON-NLS-2$
   }
 }
示例#3
0
  private boolean doBuild(
      OpenableElementInfo info,
      IProgressMonitor progressMonitor,
      Map newElements,
      IResource underlyingResource)
      throws ModelException {
    try {

      final JSSourceModuleElementInfo moduleInfo = (JSSourceModuleElementInfo) info;

      IBuffer buffer = null;
      // ensure buffer is opened
      if (hasBuffer()) {
        buffer = getBufferManager().getBuffer(this);
        if (buffer == null) {
          buffer = openBuffer(progressMonitor, moduleInfo);
        }
      }

      final char[] contents = (buffer == null) ? null : buffer.getCharacters();

      // generate structure and compute syntax problems if needed
      final VjoSourceModuleStructureRequestor requestor =
          new VjoSourceModuleStructureRequestor(this, moduleInfo, newElements);

      // System.out.println("==> Parsing: " + resource.getName());
      final String natureId = getNatureId();
      if (natureId == null) {
        throw new ModelException(new ModelStatus(ModelStatus.INVALID_NAME));
      }

      SourceTypeName stName = getTypeName();
      IResource resource = getResource();
      // it is not a workspace file
      // if ("".equals(stName.groupName().trim()) && (resource == null ||
      // !resource.exists())) {
      // jstType = CodeassistUtils.findNativeJstType(stName.typeName());
      // } else {
      // processType(contents);
      // }
      final VjoSourceElementParser parser =
          (VjoSourceElementParser) getSourceElementParser(natureId);
      if (!isReadOnly()) {
        ((ISourceElementParserExtension) parser).setScriptProject(this.getScriptProject());
      }

      parser.setRequestor(requestor);
      final AccumulatingProblemReporter problemReporter = getAccumulatingProblemReporter();
      parser.setReporter(problemReporter);

      boolean reparsed = false;
      if (problemReporter != null) {
        if (!problemReporter.hasErrors()) {
          StructureBuilder.build(natureId, this, problemReporter);
          reparsed = true;
        }
        problemReporter.reportToRequestor();
      }

      if (jstType == null && isVirtualTypeResource(resource)) {

        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IResource typespaceresource = root.findMember(resource.getFullPath());
        if (typespaceresource != null) {
          URI location = typespaceresource.getLocationURI();
          String typeName = location.getPath().replace("/", ".");
          String groupName = location.getHost();
          if (typeName.indexOf(".") == 0) {
            typeName = typeName.substring(1, typeName.length());
          }
          typeName = typeName.replace(".js", "");

          jstType = CodeassistUtils.findJstType(groupName, typeName);
        }
      } else if (jstType == null || !reparsed) {
        if ("".equals(stName.groupName().trim()) && (resource == null || !resource.exists())) {
          jstType = CodeassistUtils.findNativeJstType(stName.typeName());
        } else {
          processType(contents);
        }
      }

      // parse source module after getting the JstType
      //
      SourceParserUtil.parseSourceModule(this, parser);

      if (DLTKCore.DEBUG_PRINT_MODEL) {
        System.out.println("Source Module Debug print:"); // $NON-NLS-1$

        CorePrinter printer = new CorePrinter(System.out);
        printNode(printer);
        printer.flush();
      }

      // update timestamp (might be IResource.NULL_STAMP if original does
      // not exist)
      if (underlyingResource == null) {
        underlyingResource = getResource();
      }
      // underlying resource is null in the case of a working copy out of
      // workspace
      if (underlyingResource != null) {
        moduleInfo.setTimestamp(((IFile) underlyingResource).getModificationStamp());
      }

      isConsistent = true;

      return moduleInfo.isStructureKnown();
    } catch (CoreException e) {
      throw new ModelException(e);
    }
  }