@Override
  public SearchResults find(SearchQuery query, @NotNull ProgressMonitor monitor) {
    SearchResults<SNode> searchResults = new SearchResults<SNode>();
    Object value = query.getObjectHolder().getObject();
    SModule module = null;
    if (value instanceof SModule) {
      module = ((SModule) value);
    } else if (value instanceof SModuleReference) {
      module = query.getScope().resolve(((SModuleReference) value));
    }
    if (!(module instanceof Language)) {
      return searchResults;
    }
    Language language = (Language) module;
    SModel structureModel = language.getStructureModelDescriptor();
    if (structureModel == null) {
      return searchResults;
    }
    if (!(structureModel.getRootNodes().iterator().hasNext())) {
      return searchResults;
    }
    List<SNode> roots = new LinkedList<SNode>();
    for (SNode root : structureModel.getRootNodes()) {
      roots.add(root);
    }
    searchResults.getSearchedNodes().addAll(roots);

    monitor.start("", IterableUtil.asCollection(structureModel.getRootNodes()).size() + 1);
    try {
      SearchResults<SModel> modelResults =
          FindUtils.getSearchResults(
              monitor.subTask(1),
              new SearchQuery(
                  structureModel.getReference(), GlobalScopeMinusTransient.getInstance()),
              new ModelImportsUsagesFinder());
      List<SModel> models = new ArrayList<SModel>();
      for (SearchResult<SModel> sModelSearchResult : modelResults.getSearchResults()) {
        models.add(sModelSearchResult.getObject());
      }
      SearchScope scope = new ModelsScope(models.toArray(new SModel[models.size()]));
      SearchResults<SNode> results = new SearchResults();
      for (SNode node : roots) {
        if (monitor.isCanceled()) {
          break;
        }
        results.addAll(
            FindUtils.getSearchResults(
                monitor.subTask(1),
                node,
                scope,
                "jetbrains.mps.lang.structure.findUsages.NodeUsages_Finder"));
        results.removeDuplicates();
      }
      searchResults.getSearchResults().addAll(results.getSearchResults());
      return searchResults;
    } finally {
      monitor.done();
    }
  }
Example #2
0
  public static void assertDeepModelEquals(SModel expectedModel, SModel actualModel) {
    ModelAccess.instance().checkReadAccess();

    assertSameImports(expectedModel, actualModel);
    assertSameModelImports(expectedModel, actualModel);
    assertSameLanguageAspects(expectedModel, actualModel);

    assertSameNodesCollections("root", expectedModel.getRootNodes(), actualModel.getRootNodes());
  }
Example #3
0
 public org.jetbrains.mps.openapi.model.SNode getRoot(int oneBasedIndex) {
   assert myModel != null : "call createModel() first";
   for (SNode r : myModel.getRootNodes()) {
     if (--oneBasedIndex > 0) {
       continue;
     }
     return r;
   }
   throw new IllegalArgumentException(
       Integer.toString(IterableUtil.asCollection(myModel.getRootNodes()).size()));
 }
Example #4
0
 public static Collection<String> getUsedLanguageNamespaces(
     SModel model, boolean isTemplateModel) {
   if (isTemplateModel) {
     return getUsedLanguageNamespacesInTemplateModel(model);
   }
   if (SModelStereotype.isGeneratorModel(model)) {
     TemplateModelScanner templateModelScanner = new TemplateModelScanner(model);
     templateModelScanner.scan();
     Set<String> namespaces = new HashSet<String>(templateModelScanner.getQueryLanguages());
     for (SModuleReference ref :
         ((jetbrains.mps.smodel.SModelInternal) model).engagedOnGenerationLanguages()) {
       namespaces.add(ref.getModuleName());
     }
     return namespaces;
   }
   Set<String> namespaces = new HashSet<String>();
   for (SModuleReference ref :
       ((jetbrains.mps.smodel.SModelInternal) model).engagedOnGenerationLanguages()) {
     namespaces.add(ref.getModuleName());
   }
   for (SNode root : model.getRootNodes()) {
     namespaces.add(NameUtil.namespaceFromConceptFQName(root.getConcept().getId()));
     for (SNode child : jetbrains.mps.util.SNodeOperations.getDescendants(root, null)) {
       namespaces.add(NameUtil.namespaceFromConceptFQName(child.getConcept().getId()));
     }
   }
   // empty behavior model should have it's behavior aspect descriptor generated
   if (model.getModule() instanceof Language && LanguageAspect.BEHAVIOR.is(model)) {
     namespaces.add(BootstrapLanguages.BEHAVIOR.getModuleName());
   }
   return namespaces;
 }
Example #5
0
 private void findInModel(
     SModel model,
     String prefix,
     Consumer<VirtualFile> processedConsumer,
     Consumer<SReference> consumer) {
   for (SNode root : model.getRootNodes()) {
     findPrefixReferences(prefix, root, consumer);
   }
 }
 @Override
 public ConceptInstanceMap compute() {
   if (myModelInput != null) {
     fillMap(myModelInput.getRootNodes());
   } else {
     fillMap(Collections.singletonList(myNodeInput));
   }
   return myMap;
 }
Example #7
0
 public static List<SNode> roots(SModel model, final SAbstractConcept concept) {
   if (model == null) {
     return Collections.emptyList();
   }
   List<SNode> list = new ArrayList<SNode>();
   Iterable<SNode> iterable = model.getRootNodes();
   if (concept != null) {
     Condition<SNode> cond = new InstanceOfCondition(concept).tolerateNulls();
     iterable = new ConditionalIterable<SNode>(iterable, cond);
   }
   for (SNode node : iterable) {
     list.add(node);
   }
   return list;
 }
  @Override
  public List<SNode> getNodes(String conceptFqName, boolean includeInherited) {
    // notify 'model nodes read access'
    myModel.getRootNodes().iterator();

    if (!myNodeMap.isEmpty()) {
      return getNodesImpl(conceptFqName, includeInherited);
    }
    synchronized (myNodeMap) {
      if (myNodeMap.isEmpty()) {
        ConceptInstanceMap all = build(new ConceptNodeMapBuilder(myModel));
        all.trimValues(); // merge may reuse lists,
        myNodeMap.merge(all);
      }
      return getNodesImpl(conceptFqName, includeInherited);
    }
  }
Example #9
0
 public static boolean findInModule(SModule module, String modelName, String rootName) {
   for (SModel d : module.getModels()) {
     if (d.getModelName().equals(modelName)) {
       for (SNode _n : d.getRootNodes()) {
         SNode n = (SNode) _n;
         if (SNodeOperations.isInstanceOf(n, "jetbrains.mps.baseLanguage.structure.Classifier")
             && SPropertyOperations.getString(
                     SNodeOperations.cast(n, "jetbrains.mps.baseLanguage.structure.Classifier"),
                     "name")
                 .equals(rootName)) {
           return true;
         }
       }
     }
   }
   return false;
 }
Example #10
0
 private static List<SNode> allNodesIncludingImported(
     SModel sModel, boolean roots, @Nullable SAbstractConcept concept) {
   List<SModel> modelsList = new ArrayList<SModel>();
   modelsList.add(sModel);
   List<SModel> modelDescriptors = jetbrains.mps.smodel.SModelOperations.allImportedModels(sModel);
   for (SModel descriptor : modelDescriptors) {
     modelsList.add(descriptor);
   }
   List<SNode> resultNodes = new ArrayList<SNode>();
   final Condition<SNode> instanceCondition =
       (concept == null ? null : new InstanceOfCondition(concept));
   for (SModel aModel : modelsList) {
     if (concept == null || roots) {
       ConditionalIterable<SNode> nodes =
           new ConditionalIterable<SNode>(
               (roots ? aModel.getRootNodes() : SNodeUtil.getDescendants(aModel)),
               instanceCondition);
       resultNodes.addAll(IterableUtil.asList(nodes));
     } else {
       resultNodes.addAll(jetbrains.mps.smodel.SModelOperations.getNodes(aModel, concept));
     }
   }
   return resultNodes;
 }
Example #11
0
 public int countModelNodes() {
   return countTreeNodes(myModel.getRootNodes());
 }
Example #12
0
 public static Iterable<SNode> getRootsToIterate(SModel model) {
   if (SModelStereotype.isStubModelStereotype(
       jetbrains.mps.util.SNodeOperations.getModelStereotype(model)))
     return new EmptyIterable<SNode>();
   return new ConditionalIterable<SNode>(model.getRootNodes(), new MyCondition());
 }