public static StudyCheckResult checkCodeTask(@NotNull Project project, @NotNull Task task, @NotNull StepicUser user) { int attemptId = -1; try { attemptId = getAttemptId(task); } catch (IOException e) { LOG.warn(e.getMessage()); } if (attemptId != -1) { Course course = task.getLesson().getCourse(); Language courseLanguage = course.getLanguageById(); final Editor editor = StudyUtils.getSelectedEditor(project); if (editor != null) { String commentPrefix = LanguageCommenters.INSTANCE.forLanguage(courseLanguage).getLineCommentPrefix(); final String answer = commentPrefix + PYCHARM_COMMENT + editor.getDocument().getText(); String defaultLanguage = EduPluginConfigurator.INSTANCE.forLanguage(courseLanguage).getStepikDefaultLanguage(); final StepicWrappers.SubmissionToPostWrapper submissionToPost = new StepicWrappers.SubmissionToPostWrapper(String.valueOf(attemptId), defaultLanguage, answer); return doAdaptiveCheck(submissionToPost, attemptId, user.getId()); } } else { LOG.warn("Got an incorrect attempt id: " + attemptId); } return new StudyCheckResult(StudyStatus.Unchecked, StudyCheckAction.FAILED_CHECK_LAUNCH); }
@NotNull private ChoiceTask choiceTask() { ChoiceTask task = new ChoiceTask(myName); task.setStepId(myStepId); task.addTaskText(EduNames.TASK, myStep.text); final StepicWrappers.AdaptiveAttemptWrapper.Attempt attempt = getAttemptForStep(myStepId, myUserId); if (attempt != null) { final StepicWrappers.AdaptiveAttemptWrapper.Dataset dataset = attempt.dataset; if (dataset != null) { task.setChoiceVariants(dataset.options); task.setMultipleChoice(dataset.is_multiple_choice); } else { LOG.warn("Dataset for step " + myStepId + " is null"); } } String commentPrefix = LanguageCommenters.INSTANCE.forLanguage(myLanguage).getLineCommentPrefix(); String taskFileName = getTaskFileName(myLanguage); if (taskFileName != null) { createMockTaskFile(task, commentPrefix + " you can experiment here, it won't be checked", taskFileName); } return task; }
@NotNull @Override public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { if (startOffset >= endOffset) return PsiElement.EMPTY_ARRAY; Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()); if (commenter == null || commenter.getLineCommentPrefix() == null) return PsiElement.EMPTY_ARRAY; PsiElement startElement = file.findElementAt(startOffset); if (startElement instanceof PsiWhiteSpace) startElement = startElement.getNextSibling(); PsiElement endElement = file.findElementAt(endOffset - 1); if (endElement instanceof PsiWhiteSpace) endElement = endElement.getPrevSibling(); if (startElement != null && endElement != null) { startElement = findClosestParentAfterLineBreak(startElement); if (startElement != null) { endElement = findClosestParentBeforeLineBreak(endElement); if (endElement != null) { return adjustRange(startElement, endElement); } } } return PsiElement.EMPTY_ARRAY; }
@Nullable public static Commenter getCommenter(final PsiFile file, final Editor editor, final Language lineStartLanguage, final Language lineEndLanguage) { final FileViewProvider viewProvider = file.getViewProvider(); for (MultipleLangCommentProvider provider : MultipleLangCommentProvider.EP_NAME.getExtensions()) { if (provider.canProcess(file, viewProvider)) { return provider.getLineCommenter(file, editor, lineStartLanguage, lineEndLanguage); } } final Language fileLanguage = file.getLanguage(); Language lang = lineStartLanguage == null || LanguageCommenters.INSTANCE.forLanguage(lineStartLanguage) == null || fileLanguage.getBaseLanguage() == lineStartLanguage // file language is a more specific dialect of the line language ? fileLanguage : lineStartLanguage; if (viewProvider instanceof TemplateLanguageFileViewProvider && lang == ((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage()) { lang = viewProvider.getBaseLanguage(); } return LanguageCommenters.INSTANCE.forLanguage(lang); }
/** * Analyzes position at the given offset at the given text and returns information about comments presence and kind there if any. * * @param file target file being edited (necessary for language recognition at target offset. Language is necessary * to get information about specific comment syntax) * @param chars target text * @param offset target offset at the given text * @param lineStartOffset start offset of the line that contains given offset * @return object that encapsulates information about comments at the given offset at the given text */ @NotNull public static CommentContext tryParseCommentContext(@NotNull PsiFile file, @NotNull CharSequence chars, int offset, int lineStartOffset) { Commenter langCommenter = LanguageCommenters.INSTANCE.forLanguage(PsiUtilCore.getLanguageAtOffset(file, offset)); final boolean isInsideCommentLikeCode = langCommenter instanceof CodeDocumentationAwareCommenter; if (!isInsideCommentLikeCode) { return new CommentContext(); } final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter)langCommenter; int commentStartOffset = CharArrayUtil.shiftForward(chars, lineStartOffset, " \t"); boolean docStart = commenter.getDocumentationCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentPrefix()); boolean cStyleStart = commenter.getBlockCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getBlockCommentPrefix()); boolean docAsterisk = commenter.getDocumentationCommentLinePrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentLinePrefix()); final int firstNonSpaceInLine = CharArrayUtil.shiftForward(chars, offset, " \t"); boolean slashSlash = commenter.getLineCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getLineCommentPrefix()) && firstNonSpaceInLine < chars.length() && chars.charAt(firstNonSpaceInLine) != '\n'; return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, slashSlash, commentStartOffset); }
private static void tryConvertEndOfLineComment(Document doc, PsiElement commentElement) { Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(commentElement.getLanguage()); if (commenter instanceof CodeDocumentationAwareCommenter) { CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter) commenter; String lineCommentPrefix = commenter.getLineCommentPrefix(); String blockCommentPrefix = commenter.getBlockCommentPrefix(); String blockCommentSuffix = commenter.getBlockCommentSuffix(); if (commentElement.getNode().getElementType() == docCommenter.getLineCommentTokenType() && blockCommentPrefix != null && blockCommentSuffix != null && lineCommentPrefix != null) { String commentText = StringUtil.trimStart(commentElement.getText(), lineCommentPrefix); try { Project project = commentElement.getProject(); PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project); PsiComment newComment = parserFacade.createBlockCommentFromText(commentElement.getLanguage(), commentText); commentElement.replace(newComment); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(doc); } catch (IncorrectOperationException e) { LOG.info("Failed to replace line comment with block comment", e); } } } }
@Nullable private static String getUncommentedText(@NotNull final PsiComment comment) { final PsiFile psiFile = comment.getContainingFile(); final Language language = psiFile.getViewProvider().getBaseLanguage(); final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter != null) { String text = comment.getText(); final String prefix = commenter.getBlockCommentPrefix(); if (prefix != null && text.startsWith(prefix)) { text = text.substring(prefix.length()); final String suffix = commenter.getBlockCommentSuffix(); if (suffix != null && text.length() > suffix.length()) { return text.substring(0, text.length() - suffix.length()).trim(); } } } return null; }
@NotNull @Override public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { if (startOffset >= endOffset - 1) return PsiElement.EMPTY_ARRAY; Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()); if (commenter == null || commenter.getLineCommentPrefix() == null) return PsiElement.EMPTY_ARRAY; PsiElement startElement = file.findElementAt(startOffset); if (startElement instanceof PsiWhiteSpace) startElement = startElement.getNextSibling(); PsiElement endElement = file.findElementAt(endOffset - 1); if (endElement instanceof PsiWhiteSpace) endElement = endElement.getPrevSibling(); if (startElement != null && endElement != null) { if (startElement.getTextRange().getStartOffset() > endElement.getTextRange().getStartOffset()) return PsiElement.EMPTY_ARRAY; startElement = findClosestParentAfterLineBreak(startElement); if (startElement != null) { endElement = findClosestParentBeforeLineBreak(endElement); if (endElement != null) { PsiElement commonParent = startElement.getParent(); if (endElement.getParent() == commonParent) { if (startElement == endElement) return new PsiElement[] {startElement}; return new PsiElement[] {startElement, endElement}; } } } } return PsiElement.EMPTY_ARRAY; }
/** * Analyzes position at the given offset at the given text and returns information about comments presence and kind there if any. * * @param file target file being edited (necessary for language recognition at target offset. Language is necessary * to get information about specific comment syntax) * @param chars target text * @param offset target offset at the given text * @param lineStartOffset start offset of the line that contains given offset * @return object that encapsulates information about comments at the given offset at the given text */ @NotNull public static CommentContext tryParseCommentContext(@NotNull PsiFile file, @NotNull CharSequence chars, int offset, int lineStartOffset) { Commenter langCommenter = LanguageCommenters.INSTANCE.forLanguage(PsiUtilBase.getLanguageAtOffset(file, offset)); final boolean isInsideCommentLikeCode = langCommenter instanceof CodeDocumentationAwareCommenter; if (!isInsideCommentLikeCode) { return new CommentContext(); } final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter)langCommenter; int commentStartOffset = CharArrayUtil.shiftForward(chars, lineStartOffset, " \t"); boolean docStart = commenter.getDocumentationCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentPrefix()); boolean cStyleStart = commenter.getBlockCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getBlockCommentPrefix()); boolean docAsterisk = commenter.getDocumentationCommentLinePrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentLinePrefix()); final int firstNonSpaceInLine = CharArrayUtil.shiftForward(chars, offset, " \t"); boolean slashSlash = commenter.getLineCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getLineCommentPrefix()) && firstNonSpaceInLine < chars.length() && chars.charAt(firstNonSpaceInLine) != '\n'; return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, slashSlash, commentStartOffset); }
private boolean isCommentToken(PsiElement e, CharSequence token) { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(HaxeLanguage.INSTANCE); assert(commenter instanceof HaxeCommenter); final IElementType tokenType = e.getNode().getElementType(); if (tokenType == HaxeTokenTypeSets.DOC_COMMENT) { // XXX: Should we be checking that the token is at the beginning or end of the element? // Or, that the line prefix is actually the first thing on the line? return ((HaxeCommenter)commenter).getDocumentationCommentLinePrefix().contentEquals(token) || ((HaxeCommenter)commenter).getDocumentationCommentPrefix().contentEquals(token) || ((HaxeCommenter)commenter).getDocumentationCommentSuffix().contentEquals(token) // A lot of folks don't use the proper doc comment terminator "**/", and the compiler // accepts a normal block comment terminator "*/". || commenter.getBlockCommentSuffix().contentEquals(token); } else if (tokenType == HaxeTokenTypeSets.MML_COMMENT) { return commenter.getBlockCommentPrefix().contentEquals(token) || commenter.getBlockCommentSuffix().contentEquals(token); } return commenter.getLineCommentPrefix().contentEquals(token); }
@Nonnull @Override public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { if (startOffset >= endOffset - 1) return PsiElement.EMPTY_ARRAY; Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()); if (commenter == null || commenter.getLineCommentPrefix() == null) return PsiElement.EMPTY_ARRAY; PsiElement startElement = file.findElementAt(startOffset); if (startElement instanceof PsiWhiteSpace) startElement = startElement.getNextSibling(); PsiElement endElement = file.findElementAt(endOffset - 1); if (endElement instanceof PsiWhiteSpace) endElement = endElement.getPrevSibling(); if (startElement != null && endElement != null) { if (startElement.getTextRange().getStartOffset() > endElement.getTextRange().getStartOffset()) return PsiElement.EMPTY_ARRAY; startElement = findClosestParentAfterLineBreak(startElement); if (startElement != null) { endElement = findClosestParentBeforeLineBreak(endElement); if (endElement != null) { PsiElement commonParent = startElement.getParent(); if (endElement.getParent() == commonParent) { if (startElement == endElement) return new PsiElement[] {startElement}; return new PsiElement[] {startElement, endElement}; } } } } return PsiElement.EMPTY_ARRAY; }
@NotNull private TheoryTask theoryTask() { TheoryTask task = new TheoryTask(myName); task.setStepId(myStepId); task.addTaskText(EduNames.TASK, myStep.text); String commentPrefix = LanguageCommenters.INSTANCE.forLanguage(myLanguage).getLineCommentPrefix(); String taskFileName = getTaskFileName(myLanguage); if (taskFileName != null) { createMockTaskFile(task, commentPrefix + " this is a theory task. You can use this editor as a playground", taskFileName); } return task; }
@Nullable public static Couple<String> getBlockPrefixSuffixPair(@NotNull PsiElement comment) { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage()); if (commenter != null) { final String prefix = commenter.getBlockCommentPrefix(); final String suffix = commenter.getBlockCommentSuffix(); if (prefix != null || suffix != null) { return Couple.of(StringUtil.notNullize(prefix), StringUtil.notNullize(suffix)); } } return null; }
@Override public boolean isOneLine() { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(myHost.getLanguage()); if (commenter instanceof CodeDocumentationAwareCommenter) { return myHost.getTokenType() == ((CodeDocumentationAwareCommenter) commenter).getLineCommentTokenType(); } return false; }
@Override protected boolean isValidFor(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull final PsiFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof AbstractFileType) { return ((AbstractFileType)fileType).getCommenter() != null; } Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()); if (commenter == null) commenter = LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage()); if (commenter == null) return false; return commenter.getBlockCommentPrefix() != null && commenter.getBlockCommentSuffix() != null; }
@Override protected boolean isValidFor(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull final PsiFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof AbstractFileType) { return ((AbstractFileType)fileType).getCommenter() != null; } if (LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()) != null || LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage()) != null) return true; PsiElement host = InjectedLanguageManager.getInstance(project).getInjectionHost(file); return host != null && LanguageCommenters.INSTANCE.forLanguage(host.getLanguage()) != null; }
@Override public boolean canSelect(PsiElement e) { if (e instanceof PsiComment) { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(e.getLanguage()); if (!(commenter instanceof CodeDocumentationAwareCommenter)) return true; return !((CodeDocumentationAwareCommenter) commenter).isDocumentationComment((PsiComment)e); } return false; }
private ArrangementSectionRuleManager(@NotNull Language language, @NotNull ArrangementStandardSettingsManager settingsManager, @NotNull ArrangementColorsProvider colorsProvider, @NotNull ArrangementSectionRulesControl control) { myCommenter = LanguageCommenters.INSTANCE.forLanguage(language); myControl = control; final List<CompositeArrangementSettingsToken> tokens = ContainerUtil.newArrayList(); tokens.add(new CompositeArrangementSettingsToken(TYPE, ContainerUtil.newArrayList(START_SECTION, END_SECTION))); tokens.add(new CompositeArrangementSettingsToken(TEXT)); myEditor = new ArrangementMatchingRuleEditor(settingsManager, tokens, colorsProvider, control); }
private static boolean hasAnyDocAwareCommenters() { final Collection<Language> languages = Language.getRegisteredLanguages(); for (Language language : languages) { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter instanceof CodeDocumentationAwareCommenter) { final CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter)commenter; if (docCommenter.getDocumentationCommentLinePrefix() != null) { return true; } } } return false; }
@Override public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { final PsiElement psiElement = descriptor.getPsiElement(); final PsiFile psiFile = psiElement.getContainingFile(); new WriteCommandAction(project, psiFile) { @Override protected void run(@NotNull final Result result) throws Throwable { final XmlTag tag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class); if (tag == null) return; final Language language = psiFile.getViewProvider().getBaseLanguage(); final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter == null) return; final PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("dummy", language.getAssociatedFileType(), commenter.getBlockCommentPrefix() + "@declare id=\"" + myId + "\"" + commenter.getBlockCommentSuffix() + "\n"); final XmlTag parent = tag.getParentTag(); if (parent != null && parent.isValid()) { final XmlTag[] tags = parent.getSubTags(); if (tags.length > 0) { final PsiFile psi = tempFile.getViewProvider().getPsi(language); if (psi != null) { final PsiElement element = psi.findElementAt(1); if (element instanceof PsiComment) { parent.getNode().addChild(element.getNode(), tags[0].getNode()); } } } } } }.execute(); }
public ApplicationEnvironment(CoreApplicationEnvironment appEnvironment) { appEnvironment.registerFileType(PropertiesFileType.INSTANCE, "properties"); appEnvironment.addExplicitExtension(SyntaxHighlighterFactory.LANGUAGE_FACTORY, PropertiesLanguage.INSTANCE, new PropertiesSyntaxHighlighterFactory()); appEnvironment.addExplicitExtension(LanguageParserDefinitions.INSTANCE, PropertiesLanguage.INSTANCE, new PropertiesParserDefinition()); appEnvironment.addExtension(FileBasedIndexExtension.EXTENSION_POINT_NAME, new XmlPropertiesIndex()); appEnvironment.addExtension(StubIndexExtension.EP_NAME, new PropertyKeyIndex()); appEnvironment.registerApplicationService(PropertiesQuickFixFactory.class, new EmptyPropertiesQuickFixFactory()); appEnvironment.registerApplicationService(PropertiesRefactoringSettings.class, new PropertiesRefactoringSettings()); appEnvironment.addExplicitExtension(LanguageAnnotators.INSTANCE, PropertiesLanguage.INSTANCE, new PropertiesAnnotator()); appEnvironment.addExplicitExtension(LanguageFindUsages.INSTANCE, PropertiesLanguage.INSTANCE, new PropertiesFindUsagesProvider()); appEnvironment.addExplicitExtension(LanguageASTFactory.INSTANCE, PropertiesLanguage.INSTANCE, new PropertiesASTFactory()); appEnvironment.addExplicitExtension(LanguageFolding.INSTANCE, PropertiesLanguage.INSTANCE, new PropertiesFoldingBuilder()); appEnvironment.addExplicitExtension(ElementManipulators.INSTANCE, PropertyImpl.class, new PropertyManipulator()); appEnvironment.addExplicitExtension(ElementManipulators.INSTANCE, PropertyKeyImpl.class, new PropertyKeyManipulator()); appEnvironment.addExplicitExtension(ElementManipulators.INSTANCE, PropertyValueImpl.class, new PropertyValueManipulator()); final StubElementTypeHolderEP stubElementTypeHolderBean = new StubElementTypeHolderEP(); stubElementTypeHolderBean.holderClass = PropertiesElementTypes.class.getName(); appEnvironment.addExtension(StubElementTypeHolderEP.EP_NAME, stubElementTypeHolderBean); appEnvironment.addExplicitExtension(LanguageCommenters.INSTANCE, PropertiesLanguage.INSTANCE, new PropertiesCommenter()); appEnvironment.addExplicitExtension(IdIndexers.INSTANCE, PropertiesFileType.INSTANCE, new PropertiesIdIndexer()); appEnvironment.addExplicitExtension(TodoIndexers.INSTANCE, PropertiesFileType.INSTANCE, new PropertiesTodoIndexer()); appEnvironment.addExtension(IconProvider.EXTENSION_POINT_NAME, new XmlPropertiesIconProvider()); }
private static Commenter getCommenter(FileType fileType) { if (fileType instanceof LanguageFileType) { return LanguageCommenters.INSTANCE.forLanguage(((LanguageFileType)fileType).getLanguage()); } return null; }
@Nullable public static Pair<String, String> getBlockPrefixSuffixPair(PsiElement comment) { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage()); if (commenter != null) { final String prefix = commenter.getBlockCommentPrefix(); final String suffix = commenter.getBlockCommentSuffix(); if (prefix != null || suffix != null) { return Pair.create(StringUtil.notNullize(prefix), StringUtil.notNullize(suffix)); } } return null; }
@Override public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException { if (elements.length == 0) return null; PsiElement firstElement = elements[0]; PsiElement lastElement = elements[elements.length - 1]; PsiFile psiFile = firstElement.getContainingFile(); Language language = psiFile.getLanguage(); Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter == null) return null; String linePrefix = commenter.getLineCommentPrefix(); if (linePrefix == null) return null; int prefixLength = linePrefix.length(); int startOffset = firstElement.getTextRange().getStartOffset(); int endOffset = lastElement.getTextRange().getEndOffset(); int delta = 0; TextRange rangeToSelect = new TextRange(startOffset, startOffset); String startText = myProvider.getStartString(); int descPos = startText.indexOf("?"); if (descPos >= 0) { startText = startText.replace("?", DEFAULT_DESC_TEXT); rangeToSelect = new TextRange(startOffset + descPos, startOffset + descPos + DEFAULT_DESC_TEXT.length()); } String startString = linePrefix + startText + "\n"; String endString = "\n" + linePrefix + myProvider.getEndString(); editor.getDocument().insertString(endOffset, endString); delta += endString.length(); editor.getDocument().insertString(startOffset, startString); delta += startString.length(); rangeToSelect = rangeToSelect.shiftRight(prefixLength); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); documentManager.commitDocument(documentManager.getDocument(psiFile)); adjustLineIndent(project, psiFile, language, new TextRange(endOffset + delta - endString.length(), endOffset + delta)); adjustLineIndent(project, psiFile, language, new TextRange(startOffset, startOffset + startString.length())); return rangeToSelect; }
@Override protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull final PsiFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof AbstractFileType) { return ((AbstractFileType)fileType).getCommenter() != null; } Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()); if (commenter == null) commenter = LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage()); if (commenter == null) return false; return commenter.getBlockCommentPrefix() != null && commenter.getBlockCommentSuffix() != null; }
@Override protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull final PsiFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof AbstractFileType) { return ((AbstractFileType)fileType).getCommenter() != null; } return LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()) != null || LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage()) != null; }
public boolean isOneLine() { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(myHost.getLanguage()); if (commenter instanceof CodeDocumentationAwareCommenter) { return myHost.getTokenType() == ((CodeDocumentationAwareCommenter) commenter).getLineCommentTokenType(); } return false; }
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { final PsiElement psiElement = descriptor.getPsiElement(); final PsiFile psiFile = psiElement.getContainingFile(); new WriteCommandAction(project, psiFile) { protected void run(final Result result) throws Throwable { final XmlTag tag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class); if (tag == null) return; final Language language = psiFile.getViewProvider().getBaseLanguage(); final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter == null) return; final PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("dummy", language.getAssociatedFileType(), commenter.getBlockCommentPrefix() + "@declare id=\"" + myId + "\"" + commenter.getBlockCommentSuffix() + "\n"); final XmlTag parent = tag.getParentTag(); if (parent != null && parent.isValid()) { final XmlTag[] tags = parent.getSubTags(); if (tags.length > 0) { final PsiFile psi = tempFile.getViewProvider().getPsi(language); if (psi != null) { final PsiElement element = psi.findElementAt(1); if (element instanceof PsiComment) { parent.getNode().addChild(element.getNode(), tags[0].getNode()); } } } } } }.execute(); }
@Nullable public static Couple<String> getBlockPrefixSuffixPair(PsiElement comment) { final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage()); if (commenter != null) { final String prefix = commenter.getBlockCommentPrefix(); final String suffix = commenter.getBlockCommentSuffix(); if (prefix != null || suffix != null) { return Couple.of(StringUtil.notNullize(prefix), StringUtil.notNullize(suffix)); } } return null; }
private static String getLineCommentPrefix(IElementType token) { if (token == null) return null; Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(token.getLanguage()); return (commenter instanceof CodeDocumentationAwareCommenter) && token.equals(((CodeDocumentationAwareCommenter)commenter).getLineCommentTokenType()) ? commenter.getLineCommentPrefix() : null; }
@Override public TextRange surroundElements(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiElement[] elements) throws IncorrectOperationException { if (elements.length == 0) return null; PsiElement firstElement = elements[0]; PsiElement lastElement = elements[elements.length - 1]; PsiFile psiFile = firstElement.getContainingFile(); Language language = psiFile.getLanguage(); Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter == null) return null; String linePrefix = commenter.getLineCommentPrefix(); if (linePrefix == null) return null; int prefixLength = linePrefix.length(); int startOffset = firstElement.getTextRange().getStartOffset(); int endOffset = lastElement.getTextRange().getEndOffset(); int delta = 0; TextRange rangeToSelect = new TextRange(startOffset, startOffset); String startText = myProvider.getStartString(); int descPos = startText.indexOf("?"); if (descPos >= 0) { startText = startText.replace("?", DEFAULT_DESC_TEXT); rangeToSelect = new TextRange(startOffset + descPos, startOffset + descPos + DEFAULT_DESC_TEXT.length()); } String startString = linePrefix + startText + "\n"; String endString = "\n" + linePrefix + myProvider.getEndString(); editor.getDocument().insertString(endOffset, endString); delta += endString.length(); editor.getDocument().insertString(startOffset, startString); delta += startString.length(); rangeToSelect = rangeToSelect.shiftRight(prefixLength); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); documentManager.commitDocument(documentManager.getDocument(psiFile)); adjustLineIndent(project, psiFile, language, new TextRange(endOffset + delta - endString.length(), endOffset + delta)); adjustLineIndent(project, psiFile, language, new TextRange(startOffset, startOffset + startString.length())); return rangeToSelect; }