@Override
  public void launch(IEditorPart editor, final String mode) {
    Activator.logInfo("launch shortcut: editor" + editor.getTitle() + " mode " + mode);

    IEditorInput ei = editor.getEditorInput();

    if (ei != null && ei instanceof FileEditorInput) {
      FileEditorInput fei = (FileEditorInput) ei;
      IFile file = fei.getFile();
      final String fName = file.getProjectRelativePath().toOSString();
      final String pName = file.getProject().getName();
      launch(pName, fName, mode);
    }
  }
  private void launch(String pName, String fName, String mode) {
    try {
      ILaunchConfiguration found = null;
      ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
      ILaunchConfigurationType lct =
          lm.getLaunchConfigurationType(GoLaunchConfigurationDelegate.ID);
      ILaunchConfiguration[] lcfgs = lm.getLaunchConfigurations(lct);

      for (ILaunchConfiguration lcf : lcfgs) {
        String project = lcf.getAttribute(GoConstants.GO_CONF_ATTRIBUTE_PROJECT, "");
        String mainfile = lcf.getAttribute(GoConstants.GO_CONF_ATTRIBUTE_MAIN, "");
        String prgArgs = lcf.getAttribute(GoConstants.GO_CONF_ATTRIBUTE_ARGS, "");

        if (prgArgs.isEmpty()) {
          // this is an empty run, no params, don't mix with already
          // definded with params
          if (project.equalsIgnoreCase(pName)
              && Path.fromOSString(fName).equals(Path.fromOSString(mainfile))) {
            found = lcf;
            break;
          }
        }
      }

      if (found == null) {
        // create a new launch configuration
        String cfgName = lm.generateLaunchConfigurationName(Path.fromOSString(fName).lastSegment());
        ILaunchConfigurationWorkingCopy workingCopy = lct.newInstance(null, cfgName);
        workingCopy.setAttribute(GoConstants.GO_CONF_ATTRIBUTE_PROJECT, pName);
        workingCopy.setAttribute(GoConstants.GO_CONF_ATTRIBUTE_MAIN, fName);
        workingCopy.setAttribute(GoConstants.GO_CONF_ATTRIBUTE_ARGS, "");
        workingCopy.setAttribute(
            GoConstants.GO_CONF_ATTRIBUTE_BUILD_CONFIG, BuildConfiguration.RELEASE.toString());
        workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
        workingCopy.setAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, "UTF-8");

        found = workingCopy.doSave();
      }

      if (found != null) {
        found.launch(mode, null, true, true);
      }
    } catch (CoreException ce) {
      Activator.logError(ce);
    }
  }
  @Override
  public void launch(ISelection selection, final String mode) {
    Activator.logInfo("launch shortcut: selection " + selection + " mode " + mode);

    if (selection != null) {
      if (selection instanceof TreeSelection) {
        TreeSelection ts = (TreeSelection) selection;

        Object e = ts.getFirstElement();

        if (e instanceof IFile) {
          IFile file = (IFile) e;
          final String fName = file.getProjectRelativePath().toOSString();
          final String pName = file.getProject().getName();
          launch(pName, fName, mode);
        }
      }
    }
  }
Esempio n. 4
0
  public static void main(String[] args) {

    Lexer lexer = new Lexer();
    Tokenizer tokenizer = new Tokenizer(lexer);
    VariableParser fparser = new VariableParser(tokenizer, null, null);

    try {
      lexer.scan(new File("test/test_go/import_test.go"));
      for (Var var : fparser.vars) {
        Activator.logInfo("=================================================");
        Activator.logInfo(var.getDocumentation());
        Activator.logInfo("-------------------------------------------------");
        Activator.logInfo(var.getName());
        Activator.logInfo(var.getInsertionText());
        Activator.logInfo("-------------------------------------------------");
      }

    } catch (IOException e) {
      Activator.logError(e);
    }
  }
/**
 * Implement an IContentAssistProcessor (and IContentAssistProcessorExt), and delegate the work
 * through to the GoCodeClient class. This IContentAssistProcessor is declared in the plugin.xml,
 * and is called from the GoEditorSourceViewerConfiguration class in the main GoClipse plugin.
 */
public class GocodeContentAssistProcessor implements IContentAssistProcessorExt {

  private GocodeClient client = new GocodeClient();

  private static HashMap<String, CodeContext> codeContexts = new HashMap<String, CodeContext>();

  private Image defaultImage =
      Activator.getImageDescriptor("icons/orange_cube16.png").createImage();
  private Image funcImage = Activator.getImageDescriptor("icons/function_co.png").createImage();
  private Image privateFuncImage =
      Activator.getImageDescriptor("icons/public_co.gif").createImage();
  private Image interfaceImage = Activator.getImageDescriptor("icons/interface.gif").createImage();
  private Image structImage = Activator.getImageDescriptor("icons/struct.png").createImage();
  private Image importImage = Activator.getImageDescriptor("icons/imp_obj.gif").createImage();
  private Image privateVarImage =
      Activator.getImageDescriptor("icons/field_private_obj.gif").createImage();
  private Image publicVarImage =
      Activator.getImageDescriptor("icons/field_public_obj.gif").createImage();

  @SuppressWarnings("unused")
  private Image localVarImage =
      Activator.getImageDescriptor("icons/variable_local_obj.gif").createImage();

  private IEditorPart editor;

  /** */
  public GocodeContentAssistProcessor() {}

  @Override
  public void setEditorContext(IEditorPart editor) {
    this.editor = editor;
  }

  @Override
  public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    IPath path = null;

    try {
      IEditorInput input = editor.getEditorInput();

      if (input instanceof IPathEditorInput) {
        IPathEditorInput pathInput = (IPathEditorInput) input;
        path = pathInput.getPath();
      }
    } catch (NullPointerException npe) {

    }

    ArrayList<ICompletionProposal> results = new ArrayList<ICompletionProposal>();

    if (path != null) {
      String filename = path.toOSString();
      IDocument document = viewer.getDocument();
      CodeContext codeContext = codeContexts.get(filename);

      @SuppressWarnings("unused")
      int linenumber = 0;

      try {
        // the following starts on line 0?, so we add 1 to the result
        linenumber = document.getLineOfOffset(offset) + 1;
      } catch (BadLocationException e1) {
        GocodePlugin.logError(e1);
      }

      if (codeContext == null) {
        try {
          codeContext = CodeContext.getCodeContext(getProjectFor(editor), filename, document.get());
        } catch (IOException e) {
          GocodePlugin.logError(e);
        }
      }

      if (path != null) {
        String fileName = path.toOSString();

        List<String> completions =
            client.getCompletions(getProjectFor(editor), fileName, document.get(), offset);

        if (completions == null) {
          completions = Collections.emptyList();
        }

        for (String string : completions) {
          String prefix = "";
          prefix = lastWord(document, offset);
          int firstComma = string.indexOf(",,");
          int secondComma = string.indexOf(",,", firstComma + 2);

          if (firstComma != -1 && secondComma != -1) {
            String type = string.substring(0, firstComma);

            if ("PANIC".equals(type)) {
              GocodePlugin.logError("PANIC from gocode - likely go/gocode version mismatch?");
              continue;
            }

            String identifier = string.substring(firstComma + 2, secondComma);

            if ("PANIC".equals(identifier)) {
              GocodePlugin.logError("PANIC from gocode - likely go/gocode version mismatch?");
              continue;
            }

            String spec = string.substring(secondComma + 2);

            String descriptiveString = identifier + " : " + spec;
            String description = codeContext.getDescriptionForName(identifier).trim();
            IContextInformation info = new ContextInformation(description, description);
            // MessageFormat.format(JavaEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$

            Image image = defaultImage;
            String substr = identifier.substring(prefix.length());
            int replacementLength = identifier.length() - prefix.length();

            if (descriptiveString != null && descriptiveString.contains(" : func")) {
              if (codeContext.isMethodName(identifier)) {
                image = privateFuncImage;

              } else {
                image = funcImage;
              }

              substr = identifier.substring(prefix.length()) + "()";
              replacementLength++;

            } else if (descriptiveString != null && descriptiveString.contains(" : interface")) {
              image = interfaceImage;

            } else if (descriptiveString != null && descriptiveString.contains(" : struct")) {
              image = structImage;

            } else if ("package".equals(type)) {
              image = importImage;

              substr = identifier.substring(prefix.length()) + ".";
              replacementLength++;
            } else {
              if (substr != null
                  && substr.length() > 0
                  && Character.isUpperCase(substr.charAt(0))) {
                image = publicVarImage;

              } else {
                image = privateVarImage;
              }
            }

            // format the output
            descriptiveString =
                descriptiveString
                    .replace(" : func", " ")
                    .replace(" : interface", " ")
                    .replace(" : struct", " ")
                    .replace("(", "( ")
                    .replace(")", " )");

            results.add(
                new CompletionProposal(
                    identifier,
                    offset - prefix.length(),
                    prefix.length(),
                    identifier.length(),
                    image,
                    descriptiveString,
                    info,
                    description));
          }
        }
      }
    }

    return results.toArray(new ICompletionProposal[] {});
  }

  /**
   * @param editor
   * @return
   */
  private IProject getProjectFor(IEditorPart editor) {
    if (editor == null) {
      return null;
    }

    IResource resource = (IResource) editor.getEditorInput().getAdapter(IResource.class);

    return resource != null ? resource.getProject() : null;
  }

  /**
   * @param doc
   * @param offset
   * @return
   */
  private static String lastWord(IDocument doc, int offset) {
    try {
      for (int n = offset - 1; n >= 0; n--) {
        char c = doc.getChar(n);

        if (!Character.isJavaIdentifierPart(c)) {
          return doc.get(n + 1, offset - n - 1);
        }
      }
    } catch (BadLocationException e) {
      GocodePlugin.logError(e);
    }

    return "";
  }

  @Override
  public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
    return null;
  }

  @Override
  public char[] getCompletionProposalAutoActivationCharacters() {
    return new char[] {'.'};
  }

  @Override
  public char[] getContextInformationAutoActivationCharacters() {
    return null;
  }

  @Override
  public String getErrorMessage() {
    return client.getError();
  }

  @Override
  public IContextInformationValidator getContextInformationValidator() {
    return null;
  }
}