示例#1
0
  @NotNull
  public static Set<SModuleReference> getModules(SRepository repository, final Library library) {
    if (!ModuleLibraryType.isModuleLibrary(library)) {
      return Collections.emptySet();
    }
    final Set<SModuleReference> modules = new HashSet<SModuleReference>();
    final Set<IFile> moduleXmls = new HashSet<IFile>();
    for (VirtualFile file : library.getFiles(ModuleXmlRootDetector.MPS_MODULE_XML)) {
      moduleXmls.add(VirtualFileUtils.toIFile(file));
    }

    repository
        .getModelAccess()
        .runReadAction(
            new Runnable() {
              @Override
              public void run() {
                for (IFile moduleDescriptor : moduleXmls) {
                  SModule moduleByFile =
                      ModuleFileTracker.getInstance().getModuleByFile(moduleDescriptor);
                  if (moduleByFile != null) {
                    modules.add(moduleByFile.getModuleReference());
                  }
                }
              }
            });
    return modules;
  }
示例#2
0
  public static SModel doModelParsing(FileContent inputData) {
    SModel model = inputData.getUserData(PARSED_MODEL);

    if (model == null) {
      String ext = FileUtil.getExtension(inputData.getFileName());
      if (MPSFileTypeFactory.MPS_ROOT_FILE_TYPE.equals(inputData.getFile().getFileType())) {
        ext = MPSFileTypeFactory.MPS_HEADER_FILE_TYPE.getDefaultExtension();
      }
      ModelFactory factory = PersistenceFacade.getInstance().getModelFactory(ext);
      if (factory == null) {
        return null;
      }
      if (factory instanceof FolderModelFactory) {
        model =
            PersistenceUtil.loadModel(
                VirtualFileUtils.toIFile(
                    MPSFileTypeFactory.MPS_ROOT_FILE_TYPE.equals(inputData.getFile().getFileType())
                        ? inputData.getFile().getParent().findChild(MPSExtentions.DOT_MODEL_HEADER)
                        : inputData.getFile()));
      } else {
        model =
            factory.isBinary()
                ? PersistenceUtil.loadModel(inputData.getContent(), ext)
                : PersistenceUtil.loadModel(inputData.getContentAsText().toString(), ext);
      }
      if (model == null) {
        return null;
      }
      inputData.putUserData(PARSED_MODEL, model);
    }
    return model;
  }
示例#3
0
 private static boolean hasModule(Library library, SModule module) {
   if (!isSuitableModule(module) || !ModuleLibraryType.isModuleLibrary(library)) {
     return false;
   }
   Solution solution = (Solution) module;
   return Arrays.asList(library.getFiles(ModuleXmlRootDetector.MPS_MODULE_XML))
       .contains(VirtualFileUtils.getOrCreateVirtualFile(solution.getDescriptorFile()));
 }
示例#4
0
 @Override
 public void fileStatusChanged(@NotNull VirtualFile file) {
   IFile ifile = VirtualFileUtils.toIFile(file);
   SModel emd = SModelFileTracker.getInstance().findModel(ifile);
   if (emd != null) {
     rehighlightFeatureAndDescendants(new ModelFeature(emd.getReference()));
   }
 }
  @Override
  public void findTargets(
      TargetKind kind,
      Collection<SModel> scope,
      Consumer<NavigationTarget> consumer,
      Consumer<SModel> processedConsumer) {
    final ID<Integer, List<SNodeDescriptor>> indexName = RootNodeNameIndex.NAME;
    final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();

    for (SModel sm : scope) {
      if (sm instanceof EditableSModel && ((EditableSModel) sm).isChanged()) {
        continue;
      }

      DataSource source = sm.getSource();
      if (!(source instanceof FileDataSource)) {
        continue;
      }

      IFile modelFile = ((FileDataSource) source).getFile();
      String ext = FileUtil.getExtension(modelFile.getName());
      if (ext == null
          || modelFile.isDirectory()
          || !(supportedExtensions.contains(ext.toLowerCase()))) {
        continue;
      }

      VirtualFile vf = VirtualFileUtils.getVirtualFile(modelFile);
      if (vf == null) continue; // e.g. model was deleted

      int fileId = FileBasedIndex.getFileId(vf);
      ConcreteFilesGlobalSearchScope fileScope =
          new ConcreteFilesGlobalSearchScope(Collections.singleton(vf));
      List<List<SNodeDescriptor>> descriptors =
          fileBasedIndex.getValues(indexName, fileId, fileScope);
      if (descriptors.isEmpty()) continue;

      boolean needToLoad = false;
      for (NavigationTarget snd : descriptors.get(0)) {
        PropertyConstraintsDescriptor descriptor =
            ConceptRegistry.getInstance()
                .getConstraintsDescriptor(snd.getConcept().getQualifiedName())
                .getProperty(SNodeUtil.property_INamedConcept_name);
        if (descriptor instanceof BasePropertyConstraintsDescriptor
            && !((BasePropertyConstraintsDescriptor) descriptor).isGetterDefault()) {
          needToLoad = true;
          break;
        }
      }

      if (!needToLoad) {
        for (SNodeDescriptor desc : descriptors.get(0)) {
          consumer.consume(desc);
        }
        processedConsumer.consume(sm);
      }
    }
  }
  private VirtualFile[] getSelectedFiles() {
    List<VirtualFile> selectedFilesList = new LinkedList<VirtualFile>();

    // add selected model files
    List<SModelDescriptor> descriptors = getSelectedModels();
    if (descriptors != null) {
      for (SModelDescriptor descriptor : descriptors) {
        selectedFilesList.addAll(ModelUtil.getVFilesByModelDescriptor(descriptor));
      }
    }

    // add selected modules files
    List<SModule> modules = getSelectedModules();
    if (modules != null) {
      for (SModule m : modules) {
        if (!(m instanceof AbstractModule)) continue;
        AbstractModule module = (AbstractModule) m;
        IFile home = module.getBundleHome();
        if (home != null) {
          VirtualFile vfile = VirtualFileUtils.getVirtualFile(home);
          if (vfile != null) {
            selectedFilesList.add(vfile);
          }
        }
        IFile ifile = module.getDescriptorFile();
        if (ifile != null) {
          VirtualFile vfile = VirtualFileUtils.getVirtualFile(ifile);
          if (vfile != null) {
            selectedFilesList.add(vfile);
          }
        }
      }
    }

    if (selectedFilesList.size() == 0) {
      return null;
    }

    return selectedFilesList.toArray(new VirtualFile[selectedFilesList.size()]);
  }
示例#7
0
 @Nullable
 private static FileStatus getModelFileStatus(
     @NotNull EditableSModel ed, @NotNull Project project) {
   DataSource ds = ed.getSource();
   IFile file = null;
   if (ds instanceof FileDataSource) {
     file = ((FileDataSource) ds).getFile();
   } else if (ds instanceof FilePerRootDataSource) {
     file = ((FilePerRootDataSource) ds).getFile(FilePerRootDataSource.HEADER_FILE);
   }
   VirtualFile vf = VirtualFileUtils.getVirtualFile(file);
   return (vf == null ? null : FileStatusManager.getInstance(project).getStatus(vf));
 }
示例#8
0
 public static Library getOrCreateAutoLibrary(
     AbstractModule usedModule, Project project, LibrariesContainer container) {
   Library library = getAutoLibrary(usedModule.getModuleReference(), project);
   if (library != null) {
     return library;
   }
   Set<VirtualFile> stubFiles = ModuleLibraryType.getModuleJars(usedModule);
   IFile descriptorFile = usedModule.getDescriptorFile();
   VirtualFile descriptorVirtualFile = null;
   if (descriptorFile != null) {
     descriptorVirtualFile = VirtualFileUtils.getOrCreateVirtualFile(descriptorFile);
   }
   return createAutoLibrary(
       usedModule.getModuleName(), stubFiles, descriptorVirtualFile, container);
 }
示例#9
0
 private EditableSModel getModel(AbstractTreeNode selectedNode) {
   if (selectedNode instanceof MPSPsiElementTreeNode) {
     MPSPsiNodeBase value = ((MPSPsiElementTreeNode) selectedNode).getValue();
     return getModel(value);
   } else if (selectedNode instanceof MPSPsiModelTreeNode) {
     MPSPsiModel psiModel = ((MPSPsiModelTreeNode) selectedNode).getModel();
     SModel sModel = psiModel.getSModelReference().resolve(MPSModuleRepository.getInstance());
     return (EditableSModel) sModel;
   } else if (selectedNode instanceof PsiDirectoryNode) {
     SModel sModel =
         SModelFileTracker.getInstance()
             .findModel(
                 VirtualFileUtils.toIFile(((PsiDirectoryNode) selectedNode).getVirtualFile()));
     if (sModel instanceof EditableSModel) {
       return (EditableSModel) sModel;
     }
   }
   return null;
 }
示例#10
0
  private void update(boolean force) {
    final Wrappers._boolean _force = new Wrappers._boolean(force);
    myQueue.assertSoftlyIsCommandThread();
    if (!(myDifference.isEnabled())) {
      return;
    }

    IFile modelFile = myModelDescriptor.getSource().getFile();
    if (!(modelFile.exists())) {
      return;
    }
    VirtualFile modelVFile = VirtualFileUtils.getVirtualFile(modelFile);
    if (modelVFile == null
        || ProjectLevelVcsManager.getInstance(myProject).getVcsFor(modelVFile) == null) {
      return;
    }
    FileStatus status = FileStatusManager.getInstance(myProject).getStatus(modelVFile);
    if (ConflictsUtil.isModelOrModuleConflicting(myModelDescriptor, myProject)) {
      status = FileStatus.MERGED_WITH_CONFLICTS;
    }

    if (myDifference.getChangeSet() != null) {
      ModelAccess.instance()
          .runReadAction(
              new Runnable() {
                public void run() {
                  if (myDifference.getChangeSet().getNewModel() != myModelDescriptor.getSModel()) {
                    _force.value = true;
                  }
                }
              });
    }

    if (myStatusOnLastUpdate == status && !(_force.value)) {
      return;
    }
    myDifference.removeChangeSet();
    myStatusOnLastUpdate = status;
    if (FileStatus.NOT_CHANGED == status && !(_force.value)) {
      return;
    }
    final Wrappers._T<SModel> baseVersionModel = new Wrappers._T<SModel>(null);
    if (BaseVersionUtil.isAddedFileStatus(status)
        || ConflictsUtil.isModelOrModuleConflicting(myModelDescriptor, myProject)) {
      baseVersionModel.value =
          new jetbrains.mps.smodel.SModel(myModelDescriptor.getSModelReference());
    } else {
      String content = BaseVersionUtil.getBaseVersionContent(modelVFile, myProject);
      if (content == null && status != FileStatus.NOT_CHANGED) {
        LOG.error("Base version content is null while file status is " + status);
      }
      if (content == null) {
        return;
      }
      try {
        baseVersionModel.value = ModelPersistence.readModel(content, false);
      } catch (ModelReadException e) {
        LOG.warning("", e);
        return;
      }
    }
    ModelAccess.instance()
        .runReadAction(
            new Runnable() {
              public void run() {
                synchronized (ChangesTracking.this) {
                  if (!(myDisposed)) {
                    ChangeSet changeSet =
                        ChangeSetBuilder.buildChangeSet(
                            baseVersionModel.value, myModelDescriptor.getSModel(), true);
                    myDifference.setChangeSet((ChangeSetImpl) changeSet);
                    buildCaches();
                  }
                }
              }
            });
  }
示例#11
0
  private static boolean annotate(@NotNull final EditorComponent editorComponent, boolean dryRun) {
    final LeftEditorHighlighter leftEditorHighlighter = editorComponent.getLeftEditorHighlighter();
    if (findAnnotationColumn(editorComponent) != null) {
      if (!(dryRun)) {
        leftEditorHighlighter.removeLeftColumn(findAnnotationColumn(editorComponent));
      }
      return true;
    }
    final SNode root = editorComponent.getEditedNode();
    SModel model = check_19hp0u_a0d0b(root);
    SModelDescriptor modelDescriptor = check_19hp0u_a0e0b(model);
    if (!((modelDescriptor instanceof EditableSModelDescriptor))) {
      return false;
    }
    IFile modelFile = ((EditableSModelDescriptor) modelDescriptor).getModelFile();
    if (modelFile == null) {
      return false;
    }
    final VirtualFile file = VirtualFileUtils.getVirtualFile(modelFile);
    final Project project =
        ProjectHelper.toIdeaProject(editorComponent.getOperationContext().getProject());
    final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file);
    if (vcs == null) {
      return false;
    }

    _FunctionTypes._return_P1_E0<? extends Boolean, ? super FileStatus> checkFileStatus =
        new _FunctionTypes._return_P1_E0<Boolean, FileStatus>() {
          public Boolean invoke(FileStatus fs) {
            return fs == FileStatus.UNKNOWN || fs == FileStatus.ADDED || fs == FileStatus.IGNORED;
          }
        };
    if (checkFileStatus.invoke(FileStatusManager.getInstance(project).getStatus(file))) {
      return false;
    }

    final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
    if (annotationProvider == null) {
      return false;
    }

    if (dryRun) {
      return true;
    }
    Task.Backgroundable annotateTask =
        new Task.Backgroundable(
            project, "Retrieving annotations", true, BackgroundFromStartOption.getInstance()) {
          private FileAnnotation myFileAnnotation;
          private VcsException myException;

          public void run(@NotNull ProgressIndicator indicator) {
            try {
              myFileAnnotation = annotationProvider.annotate(file);
            } catch (VcsException e) {
              myException = e;
            }
          }

          @Override
          public void onCancel() {
            onSuccess();
          }

          @Override
          public void onSuccess() {
            if (myException != null) {
              AbstractVcsHelper.getInstance(project)
                  .showErrors(Arrays.asList(myException), "Exception on retrieving annotation");
            }

            if (myFileAnnotation != null) {
              ModelAccess.instance()
                  .runReadAction(
                      new Runnable() {
                        public void run() {
                          AnnotationColumn annotationColumn =
                              new AnnotationColumn(
                                  leftEditorHighlighter, root, myFileAnnotation, vcs, file);
                          leftEditorHighlighter.addLeftColumn(annotationColumn);
                        }
                      });
            }
          }
        };
    ProgressManager.getInstance().run(annotateTask);
    return true;
  }
示例#12
0
  private <T> MultiMap<SModel, T> findCandidates(
      Collection<SModel> models,
      Set<T> elems,
      Consumer<SModel> processedModels,
      Function<T, UsageEntry> id) {
    // get all files in scope
    final ManyToManyMap<SModel, VirtualFile> scopeFiles = new ManyToManyMap<SModel, VirtualFile>();
    for (final SModel sm : models) {
      if (sm instanceof EditableSModel && ((EditableSModel) sm).isChanged()) {
        continue;
      }

      DataSource source = sm.getSource();
      // these are data sources this participant knows about
      if (!(source instanceof FileDataSource || source instanceof FilePerRootDataSource)) {
        continue;
      }

      /*
      This is a tmp fix for MPS-24151. See the issue to learn about the correct fix
       */
      if (!(sm instanceof DefaultSModelDescriptor)) {
        continue;
      }

      Collection<IFile> modelFiles = getDataSourceFiles(source);
      for (IFile modelFile : modelFiles) {
        String ext = FileUtil.getExtension(modelFile.getName());
        if (ext == null || modelFile.isDirectory()) {
          continue;
        }

        VirtualFile vf = VirtualFileUtils.getOrCreateVirtualFile(modelFile);
        if (vf == null) {
          LogManager.getLogger(MPSModelsFastFindSupport.class)
              .warn(
                  String.format(
                      "Model %s: virtual file not found for model file. Model file: %s",
                      sm.getName(), modelFile.getPath()));
          continue;
        }
        processedModels.consume(sm);
        scopeFiles.addLink(sm, vf);
      }
    }

    // filter files with usages
    ConcreteFilesGlobalSearchScope allFiles =
        new ConcreteFilesGlobalSearchScope(scopeFiles.getSecond());
    // process indexes
    MultiMap<SModel, T> result = new SetBasedMultiMap<SModel, T>();
    for (T elem : elems) {
      UsageEntry entry = id.apply(elem);

      Collection<VirtualFile> matchingFiles;

      try {
        matchingFiles = MPSModelsIndexer.getContainingFiles(entry, allFiles);
      } catch (ProcessCanceledException ce) {
        matchingFiles = Collections.emptyList();
      }

      // back-transform
      for (VirtualFile file : matchingFiles) {
        for (SModel m : scopeFiles.getBySecond(file)) {
          result.putValue(m, elem);
        }
      }
    }
    return result;
  }