Java Code Examples for com.intellij.lang.Language#getAssociatedFileType()

The following examples show how to use com.intellij.lang.Language#getAssociatedFileType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnterInStringLiteralHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isInStringLiteral(@Nonnull Editor editor, @Nonnull DataContext dataContext, int offset) {
  Language language = EnterHandler.getLanguage(dataContext);
  if (offset > 0 && language != null) {
    QuoteHandler quoteHandler = TypedHandler.getLanguageQuoteHandler(language);
    if (quoteHandler == null) {
      FileType fileType = language.getAssociatedFileType();
      quoteHandler = fileType != null ? TypedHandler.getQuoteHandlerForType(fileType) : null;
    }
    if (quoteHandler != null) {
      EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
      HighlighterIterator iterator = highlighter.createIterator(offset - 1);
      return StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()) || quoteHandler.isInsideLiteral(iterator);
    }
  }
  return false;
}
 
Example 2
Source File: DustLayeredSyntaxHighlighter.java    From Intellij-Dust with MIT License 6 votes vote down vote up
public DustLayeredSyntaxHighlighter(@Nullable Project project, @Nullable VirtualFile virtualFile, @NotNull EditorColorsScheme colors) {
  // create main highlighter
  super(new DustSyntaxHighlighter(), colors);

  // highlighter for outer lang
  FileType type = null;
  if(project == null || virtualFile == null) {
    type = StdFileTypes.PLAIN_TEXT;
  } else {
    Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(virtualFile);
    if(language != null) type = language.getAssociatedFileType();
    if(type == null) type = StdFileTypes.HTML;
  }
  SyntaxHighlighter outerHighlighter = SyntaxHighlighter.PROVIDER.create(type, project, virtualFile);

  registerLayer(DustTypes.HTML, new LayerDescriptor(outerHighlighter, ""));
}
 
Example 3
Source File: ScratchUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void updateFileExtension(@Nonnull Project project, @Nullable VirtualFile file) throws IOException {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  if (CommandProcessor.getInstance().getCurrentCommand() == null) {
    throw new AssertionError("command required");
  }

  if (file == null) return;
  Language language = LanguageUtil.getLanguageForPsi(project, file);
  FileType expected = getFileTypeFromName(file);
  FileType actual = language == null ? null : language.getAssociatedFileType();
  if (expected == actual || actual == null) return;
  String ext = actual.getDefaultExtension();
  if (StringUtil.isEmpty(ext)) return;

  String newName = PathUtil.makeFileName(file.getNameWithoutExtension(), ext);
  VirtualFile parent = file.getParent();
  newName = parent != null && parent.findChild(newName) != null ? PathUtil.makeFileName(file.getName(), ext) : newName;
  file.rename(ScratchUtil.class, newName);
}
 
Example 4
Source File: ScratchFileActions.java    From consulo with Apache License 2.0 6 votes vote down vote up
static PsiFile doCreateNewScratch(@Nonnull Project project, @Nonnull ScratchFileCreationHelper.Context context) {
  FeatureUsageTracker.getInstance().triggerFeatureUsed("scratch");
  Language language = Objects.requireNonNull(context.language);
  if (context.fileExtension == null) {
    LanguageFileType fileType = language.getAssociatedFileType();
    context.fileExtension = fileType == null ? "" : fileType.getDefaultExtension();
  }
  ScratchFileCreationHelper.EXTENSION.forLanguage(language).beforeCreate(project, context);

  VirtualFile dir = context.ideView != null ? PsiUtilCore.getVirtualFile(ArrayUtil.getFirstElement(context.ideView.getDirectories())) : null;
  RootType rootType = dir == null ? null : ScratchFileService.findRootType(dir);
  String relativePath = rootType != ScratchRootType.getInstance() ? "" : FileUtil.getRelativePath(ScratchFileService.getInstance().getRootPath(rootType), dir.getPath(), '/');

  String fileName = (StringUtil.isEmpty(relativePath) ? "" : relativePath + "/") +
                    PathUtil.makeFileName(ObjectUtils.notNull(context.filePrefix, "scratch") + (context.fileCounter != null ? context.fileCounter.create() : ""), context.fileExtension);
  VirtualFile file = ScratchRootType.getInstance().createScratchFile(project, fileName, language, context.text, context.createOption);
  if (file == null) return null;

  PsiNavigationSupport.getInstance().createNavigatable(project, file, context.caretOffset).navigate(true);
  PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
  if (context.ideView != null && psiFile != null) {
    context.ideView.selectElement(psiFile);
  }
  return psiFile;
}
 
Example 5
Source File: FluidFileType.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
private static LanguageFileType getAssociatedFileType(VirtualFile file, Project project) {
    if (project == null) {
        return null;
    }
    Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(file);

    LanguageFileType associatedFileType = null;
    if (language != null) {
        associatedFileType = language.getAssociatedFileType();
    }

    if (language == null || associatedFileType == null) {
        associatedFileType = FluidLanguage.getDefaultTemplateLang();
    }

    return associatedFileType;
}
 
Example 6
Source File: SubstitutedFileType.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static FileType substituteFileType(VirtualFile file, @Nonnull FileType fileType, Project project) {
  if (project == null) {
    return fileType;
  }
  if (fileType instanceof LanguageFileType) {
    final Language language = ((LanguageFileType)fileType).getLanguage();
    final Language substitutedLanguage = LanguageSubstitutors.INSTANCE.substituteLanguage(language, file, project);
    LanguageFileType substFileType = substitutedLanguage.getAssociatedFileType();
    if (!substitutedLanguage.equals(language) && substFileType != null) {
      return new SubstitutedFileType(fileType, substFileType, substitutedLanguage);
    }
  }

  return fileType;
}
 
Example 7
Source File: RemoteFileUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static FileType getFileType(@Nullable String contentType) {
  if (contentType == null) return null;

  int end = contentType.indexOf(';');
  String mimeType = end == -1 ? contentType : contentType.substring(0, end);
  if (mimeType.length() == 0) return null;

  for (Language language : Language.getRegisteredLanguages()) {
    String[] types = language.getMimeTypes();
    for (String type : types) {
      if (type.equalsIgnoreCase(mimeType)) {
        FileType fileType = language.getAssociatedFileType();
        if (fileType != null) {
          return fileType;
        }
      }
    }
  }
  return null;
}
 
Example 8
Source File: SoyLayeredHighlighter.java    From bamboo-soy with Apache License 2.0 6 votes vote down vote up
public SoyLayeredHighlighter(
    @Nullable Project project,
    @Nullable VirtualFile virtualFile,
    @NotNull EditorColorsScheme colors) {
  // Creating main highlighter.
  super(new SoySyntaxHighlighter(), colors);

  // Highlighter for the outer language.
  FileType type = null;
  if (project == null || virtualFile == null) {
    type = StdFileTypes.PLAIN_TEXT;
  } else {
    Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(virtualFile);
    if (language != null) type = language.getAssociatedFileType();
    if (type == null) type = SoyLanguage.getDefaultTemplateLang();
  }

  SyntaxHighlighter outerHighlighter =
      SyntaxHighlighterFactory.getSyntaxHighlighter(type, project, virtualFile);

  registerLayer(OTHER, new LayerDescriptor(outerHighlighter, ""));
}
 
Example 9
Source File: XDebuggerEditorBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setExpression(@Nullable XExpression text) {
  if (text == null) {
    text = getMode() == EvaluationMode.EXPRESSION ? XExpressionImpl.EMPTY_EXPRESSION : XExpressionImpl.EMPTY_CODE_FRAGMENT;
  }
  Language language = text.getLanguage();
  if (language == null) {
    if (myContext != null) {
      language = myContext.getLanguage();
    }
    if (language == null && mySourcePosition != null) {
      language = LanguageUtil.getFileLanguage(mySourcePosition.getFile());
    }
    if (language == null) {
      language = LanguageUtil.getFileTypeLanguage(getEditorsProvider().getFileType());
    }
    text = new XExpressionImpl(text.getExpression(), language, text.getCustomInfo(), text.getMode());
  }

  Collection<Language> languages = getSupportedLanguages();
  boolean many = languages.size() > 1;

  if (language != null) {
    myChooseFactory.setVisible(many);
  }
  myChooseFactory.setVisible(myChooseFactory.isVisible() || many);
  //myChooseFactory.setEnabled(many && languages.contains(language));

  if (language != null && language.getAssociatedFileType() != null) {
    Image fileTypeIcon = ImageEffects.layered(language.getAssociatedFileType().getIcon(), AllIcons.General.Dropdown);
    myChooseFactory.setIcon(TargetAWT.to(fileTypeIcon));
    myChooseFactory.setDisabledIcon(TargetAWT.to(ImageEffects.grayed(fileTypeIcon)));
  }

  doSetText(text);
}
 
Example 10
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static BraceMatcher getBraceMatcher(@Nonnull FileType fileType, @Nonnull Language lang) {
  PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(lang);
  if (matcher != null) {
    if (matcher instanceof XmlAwareBraceMatcher) {
      return (XmlAwareBraceMatcher)matcher;
    }
    else {
      return new PairedBraceMatcherAdapter(matcher, lang);
    }
  }

  final BraceMatcher byFileType = getBraceMatcherByFileType(fileType);
  if (byFileType != null) return byFileType;

  if (fileType instanceof LanguageFileType) {
    final Language language = ((LanguageFileType)fileType).getLanguage();
    if (lang != language) {
      final FileType type1 = lang.getAssociatedFileType();
      if (type1 != null) {
        final BraceMatcher braceMatcher = getBraceMatcherByFileType(type1);
        if (braceMatcher != null) {
          return braceMatcher;
        }
      }

      matcher = LanguageBraceMatching.INSTANCE.forLanguage(language);
      if (matcher != null) {
        return new PairedBraceMatcherAdapter(matcher, language);
      }
    }
  }

  return BraceMatcherHolder.ourDefaultBraceMatcher;
}
 
Example 11
Source File: LightVirtualFile.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setLanguage(@Nonnull Language language) {
  myLanguage = language;
  FileType type = language.getAssociatedFileType();
  if (type == null) {
    type = FileTypeRegistry.getInstance().getFileTypeByFileName(getName());
  }
  setFileType(type);
}
 
Example 12
Source File: TabbedLanguageCodeStylePanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Nonnull
@Override
protected FileType getFileType() {
  Language language = getDefaultLanguage();
  return language != null ? language.getAssociatedFileType() : PlainTextFileType.INSTANCE;
}
 
Example 13
Source File: TabbedLanguageCodeStylePanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Nonnull
@Override
protected FileType getFileType() {
  Language language = TabbedLanguageCodeStylePanel.this.getDefaultLanguage();
  return language != null ? language.getAssociatedFileType() : PlainTextFileType.INSTANCE;
}
 
Example 14
Source File: LanguageTextField.java    From consulo with Apache License 2.0 5 votes vote down vote up
public LanguageTextField(@Nullable Language language,
                         @Nonnull Project project,
                         @Nonnull String value,
                         @Nonnull DocumentCreator documentCreator,
                         boolean oneLineMode) {
  super(documentCreator.createDocument(value, language, project), project,
        language != null ? language.getAssociatedFileType() : PlainTextFileType.INSTANCE, language == null, oneLineMode);

  myLanguage = language;
  myProject = project;

  setEnabled(language != null);
}
 
Example 15
Source File: ScratchFileCreationHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiFile parseHeader(@Nonnull Project project, @Nonnull Language language, @Nonnull String text) {
  LanguageFileType fileType = language.getAssociatedFileType();
  CharSequence fileSnippet = StringUtil.first(text, 10 * 1024, false);
  PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
  return fileFactory.createFileFromText(PathUtil.makeFileName("a", fileType == null ? "" : fileType.getDefaultExtension()), language, fileSnippet);
}
 
Example 16
Source File: EditorHighlighterFactoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static LanguageFileType substituteFileType(Language language, VirtualFile vFile, Project project) {
  LanguageFileType fileType = null;
  if (vFile != null && project != null) {
    Language substLanguage = LanguageSubstitutors.INSTANCE.substituteLanguage(language, vFile, project);
    if (substLanguage != language) {
      fileType = substLanguage.getAssociatedFileType();
    }
  }
  return fileType;
}
 
Example 17
Source File: Trees.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static PsiFile createFile(Project project, Language language, String text) {
	LanguageFileType ftype = language.getAssociatedFileType();
	if ( ftype==null ) return null;
	String ext = ftype.getDefaultExtension();
	String fileName = "___fubar___."+ext; // random name but must have correct extension
	PsiFileFactoryImpl factory = (PsiFileFactoryImpl)PsiFileFactory.getInstance(project);
	return factory.createFileFromText(fileName, language, text, false, false);
}
 
Example 18
Source File: CommonCodeStyleSettings.java    From consulo with Apache License 2.0 4 votes vote down vote up
public CommonCodeStyleSettings(Language language) {
  this(language, language == null ? null : language.getAssociatedFileType());
}
 
Example 19
Source File: ScratchUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean hasMatchingExtension(@Nonnull Project project, @Nonnull VirtualFile file) {
  FileType expected = getFileTypeFromName(file);
  Language language = LanguageUtil.getLanguageForPsi(project, file);
  FileType actual = language == null ? null : language.getAssociatedFileType();
  return expected == actual && actual != null;
}
 
Example 20
Source File: ChooseByNameLanguageFilter.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
protected Image iconForFilterValue(@Nonnull Language value) {
  final LanguageFileType fileType = value.getAssociatedFileType();
  return fileType != null ? fileType.getIcon() : null;
}