@NotNull
 public static <T> Map<SNode, T> getUserObjects(TextGenBuffer buffer, String type) {
   Map<SNode, T> userObjects = (Map<SNode, T>) buffer.getUserObject(type);
   if (userObjects == null) {
     userObjects = new HashMap<SNode, T>();
     buffer.putUserObject(type, userObjects);
   }
   return userObjects;
 }
Beispiel #2
0
  /* package */ static void appendNodeText(
      TextGenBuffer buffer, SNode node, @Nullable SNode contextNode) {
    if (node == null) {
      buffer.append("???");

      if (contextNode != null) {
        buffer.foundError(
            "possible broken reference in "
                + org.jetbrains.mps.openapi.model.SNodeUtil.getDebugText(contextNode),
            contextNode,
            null);
      }

      return;
    }

    getTextGenForNode(node).doGenerateText(node, buffer);
  }
Beispiel #3
0
 // compatibility stuff
 @Deprecated
 public static void appendNodeText(SNodeTextGen textGen, SNode node, TextGenBuffer buffer) {
   textGen.setBuffer(buffer);
   try {
     textGen.setSNode(node);
     textGen.doGenerateText(node);
     textGen.setSNode(null);
   } catch (Exception e) {
     buffer.foundError(
         "failed to generate text for "
             + org.jetbrains.mps.openapi.model.SNodeUtil.getDebugText(node),
         node,
         e);
   }
 }
Beispiel #4
0
 private static List<String> getUserObjectCollection(
     String key, SNode node, TextGenBuffer buffer, Set<String> skipSet) {
   SetSequence<String> dependenciesObject = (SetSequence<String>) buffer.getUserObject(key);
   final String nodeFQName = NameUtil.nodeFQName(node);
   if (dependenciesObject != null) {
     List<String> dependencies = new ArrayList<String>(dependenciesObject.size());
     for (String dependObj : dependenciesObject) {
       if (dependObj == null || nodeFQName.equals(dependObj)) {
         continue;
       }
       if (skipSet != null && skipSet.contains(dependObj)) {
         continue;
       }
       dependencies.add(dependObj);
     }
     Collections.sort(dependencies);
     return dependencies;
   }
   return Collections.emptyList();
 }
Beispiel #5
0
  public static TextGenerationResult generateText(
      SNode node, boolean withDebugInfo, @Nullable StringBuilder[] buffers) {
    TextGenBuffer buffer = new TextGenBuffer(withDebugInfo, buffers);
    buffer.putUserObject(PACKAGE_NAME, node.getModel().getLongName());
    buffer.putUserObject(ROOT_NODE, node);
    appendNodeText(buffer, node, null);
    String topBufferText = buffer.getTopBufferText();
    int topLength =
        topBufferText.isEmpty() ? 1 : topBufferText.split(buffer.getLineSeparator(), -1).length + 2;

    // position info
    Map<SNode, TraceablePositionInfo> positionInfo = null;
    Map<SNode, ScopePositionInfo> scopeInfo = null;
    Map<SNode, UnitPositionInfo> unitInfo = null;
    if (withDebugInfo) {
      positionInfo =
          TraceInfoGenerationUtil.getUserObjects(buffer, TraceInfoGenerationUtil.POSITION_INFO);
      scopeInfo =
          TraceInfoGenerationUtil.getUserObjects(buffer, TraceInfoGenerationUtil.SCOPE_INFO);
      unitInfo = TraceInfoGenerationUtil.getUserObjects(buffer, TraceInfoGenerationUtil.UNIT_INFO);
      adjustPositions(topLength, positionInfo);
      adjustPositions(topLength, scopeInfo);
      adjustPositions(topLength, unitInfo);
    }

    // dependencies
    List<String> dependencies =
        getUserObjectCollection(
            DEPENDENCY, node, buffer, (Set<String>) buffer.getUserObject(EXTENDS));
    List<String> extend = getUserObjectCollection(EXTENDS, node, buffer, null);

    Map<String, List<String>> deps = new HashMap<String, List<String>>(2);
    deps.put(DEPENDENCY, dependencies);
    deps.put(EXTENDS, extend);

    Object result = buffer.getText();
    String outputEncoding = (String) buffer.getUserObject(OUTPUT_ENCODING);
    if (outputEncoding != null) {
      if (outputEncoding.equals("binary")) {
        result = EncodingUtil.decodeBase64((String) result);
      } else {
        try {
          result = EncodingUtil.encode((String) result, outputEncoding);
        } catch (IOException ex) {
          buffer.foundError("cannot encode the output stream", null, ex);
        }
      }
    }
    return new TextGenerationResult(
        node,
        result,
        buffer.hasErrors(),
        buffer.problems(),
        positionInfo,
        scopeInfo,
        unitInfo,
        deps);
  }