protected IScanner getScanner(IJavaScriptUnit unit) {
    if (unit.equals(fCuCache)) return fScannerCache;

    fCuCache = unit;
    IJavaScriptProject project = unit.getJavaScriptProject();
    String sourceLevel = project.getOption(JavaScriptCore.COMPILER_SOURCE, true);
    String complianceLevel = project.getOption(JavaScriptCore.COMPILER_COMPLIANCE, true);
    fScannerCache = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
    return fScannerCache;
  }
 /**
  * @return proposed variable names (may be empty, but not null). The first proposal should be used
  *     as "best guess" (if it exists).
  */
 public String[] guessConstantNames() {
   if (fGuessedConstNames == null) {
     try {
       Expression expression = getSelectedExpression().getAssociatedExpression();
       if (expression != null) {
         ITypeBinding binding = expression.resolveTypeBinding();
         fGuessedConstNames =
             StubUtility.getVariableNameSuggestions(
                 StubUtility.CONSTANT_FIELD,
                 fCu.getJavaScriptProject(),
                 binding,
                 expression,
                 Arrays.asList(getExcludedVariableNames()));
       }
     } catch (JavaScriptModelException e) {
     }
     if (fGuessedConstNames == null) fGuessedConstNames = new String[0];
   }
   return fGuessedConstNames;
 }
 private boolean isOnClassPath(IJavaScriptUnit element) throws JavaScriptModelException {
   IJavaScriptProject project = element.getJavaScriptProject();
   if (project == null || !project.exists()) return false;
   return project.isOnIncludepath(element);
 }
 public Change createChange(IProgressMonitor monitor) throws CoreException {
   final Map arguments = new HashMap();
   String project = null;
   IJavaScriptProject javaProject = fCu.getJavaScriptProject();
   if (javaProject != null) project = javaProject.getElementName();
   int flags =
       JavaScriptRefactoringDescriptor.JAR_REFACTORING
           | JavaScriptRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
   if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE)
     flags |= RefactoringDescriptor.STRUCTURAL_CHANGE;
   String pattern = ""; // $NON-NLS-1$
   try {
     pattern =
         BindingLabelProvider.getBindingLabel(
                 getContainingTypeBinding(), JavaScriptElementLabels.ALL_FULLY_QUALIFIED)
             + "."; //$NON-NLS-1$
   } catch (JavaScriptModelException exception) {
     JavaScriptPlugin.log(exception);
   }
   final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
   final String description =
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short,
           fConstantName);
   final String header =
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description,
           new String[] {pattern + fConstantName, expression});
   final JDTRefactoringDescriptorComment comment =
       new JDTRefactoringDescriptorComment(project, this, header);
   comment.addSetting(
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern,
           fConstantName));
   comment.addSetting(
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern,
           expression));
   String visibility = fVisibility;
   if ("".equals(visibility)) // $NON-NLS-1$
   visibility = RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility;
   comment.addSetting(
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility));
   if (fReplaceAllOccurrences)
     comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences);
   if (fQualifyReferencesWithDeclaringClassName)
     comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references);
   final JDTRefactoringDescriptor descriptor =
       new JDTRefactoringDescriptor(
           IJavaScriptRefactorings.EXTRACT_CONSTANT,
           project,
           description,
           comment.asString(),
           arguments,
           flags);
   arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_INPUT, descriptor.elementToHandle(fCu));
   arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_NAME, fConstantName);
   arguments.put(
       JDTRefactoringDescriptor.ATTRIBUTE_SELECTION,
       new Integer(fSelectionStart).toString()
           + " "
           + new Integer(fSelectionLength).toString()); // $NON-NLS-1$
   arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
   arguments.put(
       ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString());
   arguments.put(
       ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString());
   return new RefactoringDescriptorChange(
       descriptor,
       RefactoringCoreMessages.ExtractConstantRefactoring_name,
       new Change[] {fChange});
 }
  private void createConstantDeclaration() throws CoreException {
    Type type = getConstantType();

    IExpressionFragment fragment = getSelectedExpression();
    String initializerSource =
        fCu.getBuffer().getText(fragment.getStartPosition(), fragment.getLength());

    AST ast = fCuRewrite.getAST();
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
    variableDeclarationFragment.setInitializer(
        (Expression)
            fCuRewrite
                .getASTRewrite()
                .createStringPlaceholder(initializerSource, ASTNode.SIMPLE_NAME));

    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    fieldDeclaration.setType(type);
    Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
    if (accessModifier != null) fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));

    boolean createComments =
        JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaScriptProject())
            .createComments;
    if (createComments) {
      String comment =
          CodeGeneration.getFieldComment(
              fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
      if (comment != null && comment.length() > 0) {
        JSdoc doc =
            (JSdoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JSDOC);
        fieldDeclaration.setJavadoc(doc);
      }
    }

    AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
    ListRewrite listRewrite =
        fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
    TextEditGroup msg =
        fCuRewrite.createGroupDescription(
            RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
    if (insertFirst()) {
      listRewrite.insertFirst(fieldDeclaration, msg);
    } else {
      listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
    }

    if (fLinkedProposalModel != null) {
      ASTRewrite rewrite = fCuRewrite.getASTRewrite();
      LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
      nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);

      String[] nameSuggestions = guessConstantNames();
      if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
        nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
      }
      for (int i = 0; i < nameSuggestions.length; i++) {
        nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
      }

      LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
      typeGroup.addPosition(rewrite.track(type), true);

      ITypeBinding typeBinding = fragment.getAssociatedExpression().resolveTypeBinding();
      if (typeBinding != null) {
        ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
        for (int i = 0; i < relaxingTypes.length; i++) {
          typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
        }
      }
      ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
          fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), false);
    }
  }
 public static IFile getFile(ISourceReference ref) {
   IJavaScriptUnit unit = getCompilationUnit(ref);
   return (IFile) unit.getPrimary().getResource();
 }