Search in sources :

Example 11 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class YouTrackRepository method getIssues.

public Task[] getIssues(@Nullable String request, int max, long since) throws Exception {
    String query = getDefaultSearch();
    if (StringUtil.isNotEmpty(request)) {
        query += " " + request;
    }
    String requestUrl = "/rest/project/issues/?filter=" + encodeUrl(query) + "&max=" + max + "&updatedAfter" + since;
    HttpMethod method = doREST(requestUrl, false);
    try {
        InputStream stream = method.getResponseBodyAsStream();
        // todo workaround for http://youtrack.jetbrains.net/issue/JT-7984
        String s = StreamUtil.readText(stream, CharsetToolkit.UTF8_CHARSET);
        for (int i = 0; i < s.length(); i++) {
            if (!XMLChar.isValid(s.charAt(i))) {
                s = s.replace(s.charAt(i), ' ');
            }
        }
        Element element;
        try {
            //InputSource source = new InputSource(stream);
            //source.setEncoding("UTF-8");
            //element = new SAXBuilder(false).build(source).getRootElement();
            element = new SAXBuilder(false).build(new StringReader(s)).getRootElement();
        } catch (JDOMException e) {
            LOG.error("Can't parse YouTrack response for " + requestUrl, e);
            throw e;
        }
        if ("error".equals(element.getName())) {
            throw new Exception("Error from YouTrack for " + requestUrl + ": '" + element.getText() + "'");
        }
        List<Element> children = element.getChildren("issue");
        final List<Task> tasks = ContainerUtil.mapNotNull(children, (NullableFunction<Element, Task>) o -> createIssue(o));
        return tasks.toArray(new Task[tasks.size()]);
    } finally {
        method.releaseConnection();
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) CharsetToolkit(com.intellij.openapi.vfs.CharsetToolkit) HttpRequests(com.intellij.util.io.HttpRequests) Tag(com.intellij.util.xmlb.annotations.Tag) Date(java.util.Date) BaseRepository(com.intellij.tasks.impl.BaseRepository) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ContainerUtil(com.intellij.util.containers.ContainerUtil) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) JDOMException(org.jdom.JDOMException) Comparing(com.intellij.openapi.util.Comparing) com.intellij.tasks(com.intellij.tasks) Logger(com.intellij.openapi.diagnostic.Logger) XMLChar(org.apache.axis.utils.XMLChar) StreamUtil(com.intellij.openapi.util.io.StreamUtil) StringUtil(com.intellij.openapi.util.text.StringUtil) NullableFunction(com.intellij.util.NullableFunction) Set(java.util.Set) VersionComparatorUtil(com.intellij.util.text.VersionComparatorUtil) BaseRepositoryImpl(com.intellij.tasks.impl.BaseRepositoryImpl) TestOnly(org.jetbrains.annotations.TestOnly) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Nullable(org.jetbrains.annotations.Nullable) HttpMethod(org.apache.commons.httpclient.HttpMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) List(java.util.List) StringReader(java.io.StringReader) LocalTaskImpl(com.intellij.tasks.impl.LocalTaskImpl) HttpClient(org.apache.commons.httpclient.HttpClient) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) TaskUtil(com.intellij.tasks.impl.TaskUtil) javax.swing(javax.swing) InputStream(java.io.InputStream) SAXBuilder(org.jdom.input.SAXBuilder) InputStream(java.io.InputStream) Element(org.jdom.Element) JDOMException(org.jdom.JDOMException) JDOMException(org.jdom.JDOMException) StringReader(java.io.StringReader) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 12 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class SwitchTaskAction method removeTask.

public static void removeTask(@NotNull final Project project, LocalTask task, TaskManager manager) {
    if (task.isDefault()) {
        Messages.showInfoMessage(project, "Default task cannot be removed", "Cannot Remove");
    } else {
        List<ChangeListInfo> infos = task.getChangeLists();
        List<LocalChangeList> lists = ContainerUtil.mapNotNull(infos, (NullableFunction<ChangeListInfo, LocalChangeList>) changeListInfo -> {
            LocalChangeList changeList = ChangeListManager.getInstance(project).getChangeList(changeListInfo.id);
            return changeList != null && !changeList.isDefault() ? changeList : null;
        });
        boolean removeIt = true;
        l: for (LocalChangeList list : lists) {
            if (!list.getChanges().isEmpty()) {
                int result = Messages.showYesNoCancelDialog(project, "Changelist associated with '" + task.getSummary() + "' is not empty.\n" + "Do you want to remove it and move the changes to the active changelist?", "Changelist Not Empty", Messages.getWarningIcon());
                switch(result) {
                    case Messages.YES:
                        break l;
                    case Messages.NO:
                        removeIt = false;
                        break;
                    default:
                        return;
                }
            }
        }
        if (removeIt) {
            for (LocalChangeList list : lists) {
                ChangeListManager.getInstance(project).removeChangeList(list);
            }
        }
        manager.removeTask(task);
    }
}
Also used : TaskSettings(com.intellij.tasks.config.TaskSettings) LocalChangeList(com.intellij.openapi.vcs.changes.LocalChangeList) TaskManagerImpl(com.intellij.tasks.impl.TaskManagerImpl) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) ActionCommand(com.intellij.openapi.ui.playback.commands.ActionCommand) Comparing(com.intellij.openapi.util.Comparing) Disposer(com.intellij.openapi.util.Disposer) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) LocalTask(com.intellij.tasks.LocalTask) ComboBoxAction(com.intellij.openapi.actionSystem.ex.ComboBoxAction) DumbAware(com.intellij.openapi.project.DumbAware) DataManager(com.intellij.ide.DataManager) ChangeListManager(com.intellij.openapi.vcs.changes.ChangeListManager) StringUtil(com.intellij.openapi.util.text.StringUtil) SimpleActionGroup(com.intellij.tools.SimpleActionGroup) NullableFunction(com.intellij.util.NullableFunction) com.intellij.openapi.ui.popup(com.intellij.openapi.ui.popup) KeyEvent(java.awt.event.KeyEvent) ActionEvent(java.awt.event.ActionEvent) Disposable(com.intellij.openapi.Disposable) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) LocalTaskImpl(com.intellij.tasks.impl.LocalTaskImpl) ChangeListInfo(com.intellij.tasks.ChangeListInfo) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) Collections(java.util.Collections) TaskManager(com.intellij.tasks.TaskManager) javax.swing(javax.swing) LocalChangeList(com.intellij.openapi.vcs.changes.LocalChangeList) ChangeListInfo(com.intellij.tasks.ChangeListInfo)

Example 13 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class LighthouseRepository method getIssues.

@Override
public Task[] getIssues(@Nullable String query, int max, long since) throws Exception {
    String url = "/projects/" + myProjectId + "/tickets.xml";
    url += "?q=" + encodeUrl("state:open sort:updated ");
    if (!StringUtil.isEmpty(query)) {
        url += encodeUrl(query);
    }
    final List<Task> tasks = new ArrayList<>();
    int page = 1;
    final HttpClient client = login();
    while (tasks.size() < max) {
        HttpMethod method = doREST(url + "&page=" + page, false, client);
        InputStream stream = method.getResponseBodyAsStream();
        Element element = new SAXBuilder(false).build(stream).getRootElement();
        if ("nil-classes".equals(element.getName()))
            break;
        if (!"tickets".equals(element.getName())) {
            LOG.warn("Error fetching issues for: " + url + ", HTTP status code: " + method.getStatusCode());
            throw new Exception("Error fetching issues for: " + url + ", HTTP status code: " + method.getStatusCode() + "\n" + element.getText());
        }
        List<Element> children = element.getChildren("ticket");
        List<Task> taskList = ContainerUtil.mapNotNull(children, (NullableFunction<Element, Task>) o -> createIssue(o));
        tasks.addAll(taskList);
        page++;
    }
    return tasks.toArray(new Task[tasks.size()]);
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) TasksIcons(icons.TasksIcons) Tag(com.intellij.util.xmlb.annotations.Tag) Date(java.util.Date) BaseRepository(com.intellij.tasks.impl.BaseRepository) SimpleDateFormat(java.text.SimpleDateFormat) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) Comparing(com.intellij.openapi.util.Comparing) com.intellij.tasks(com.intellij.tasks) Logger(com.intellij.openapi.diagnostic.Logger) ParseException(java.text.ParseException) DateFormat(java.text.DateFormat) StringUtil(com.intellij.openapi.util.text.StringUtil) NullableFunction(com.intellij.util.NullableFunction) BaseRepositoryImpl(com.intellij.tasks.impl.BaseRepositoryImpl) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Nullable(org.jetbrains.annotations.Nullable) HttpMethod(org.apache.commons.httpclient.HttpMethod) List(java.util.List) HttpClient(org.apache.commons.httpclient.HttpClient) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) Element(org.jdom.Element) javax.swing(javax.swing) InputStream(java.io.InputStream) SAXBuilder(org.jdom.input.SAXBuilder) InputStream(java.io.InputStream) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) HttpClient(org.apache.commons.httpclient.HttpClient) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 14 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class ResourceBundleStructureViewComponent method getData.

@Override
public Object getData(final String dataId) {
    if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        return new ResourceBundleAsVirtualFile(myResourceBundle);
    } else if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
        return getFileEditor();
    } else if (ResourceBundle.ARRAY_DATA_KEY.is(dataId)) {
        return new ResourceBundle[] { myResourceBundle };
    } else if (IProperty.ARRAY_KEY.is(dataId)) {
        final Collection<ResourceBundleEditorViewElement> selectedElements = ((ResourceBundleEditor) getFileEditor()).getSelectedElements();
        if (selectedElements.isEmpty()) {
            return null;
        } else if (selectedElements.size() == 1) {
            return ContainerUtil.getFirstItem(selectedElements).getProperties();
        } else {
            return ContainerUtil.toArray(ContainerUtil.flatten(ContainerUtil.mapNotNull(selectedElements, (NullableFunction<ResourceBundleEditorViewElement, List<IProperty>>) element -> {
                final IProperty[] properties = element.getProperties();
                return properties == null ? null : ContainerUtil.newArrayList(properties);
            })), IProperty[]::new);
        }
    } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
        final List<PsiElement> elements = new ArrayList<>();
        Collections.addAll(elements, getSelectedPsiFiles());
        final IProperty[] properties = (IProperty[]) getData(IProperty.ARRAY_KEY.getName());
        if (properties != null) {
            for (IProperty property : properties) {
                final PsiElement element = property.getPsiElement();
                if (element.isValid()) {
                    elements.add(element);
                }
            }
        }
        return elements.toArray(new PsiElement[elements.size()]);
    } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        if (getSelectedPsiFiles().length != 0) {
            return new ResourceBundleDeleteProvider();
        }
        final IProperty[] properties = IProperty.ARRAY_KEY.getData(this);
        if (properties != null && properties.length != 0) {
            return new PropertiesDeleteProvider(((ResourceBundleEditor) getFileEditor()).getPropertiesInsertDeleteManager(), properties);
        }
    } else if (UsageView.USAGE_TARGETS_KEY.is(dataId)) {
        final PsiElement[] chosenElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
        if (chosenElements != null) {
            final UsageTarget[] usageTargets = new UsageTarget[chosenElements.length];
            for (int i = 0; i < chosenElements.length; i++) {
                usageTargets[i] = new PsiElement2UsageTargetAdapter(chosenElements[i]);
            }
            return usageTargets;
        }
    } else if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) {
        return new CopyProvider() {

            @Override
            public void performCopy(@NotNull final DataContext dataContext) {
                final PsiElement[] selectedPsiElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
                if (selectedPsiElements != null) {
                    final List<String> names = new ArrayList<>(selectedPsiElements.length);
                    for (final PsiElement element : selectedPsiElements) {
                        if (element instanceof PsiNamedElement) {
                            names.add(((PsiNamedElement) element).getName());
                        }
                    }
                    CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
                }
            }

            @Override
            public boolean isCopyEnabled(@NotNull final DataContext dataContext) {
                return true;
            }

            @Override
            public boolean isCopyVisible(@NotNull final DataContext dataContext) {
                return true;
            }
        };
    }
    return super.getData(dataId);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PopupHandler(com.intellij.ui.PopupHandler) NonNls(org.jetbrains.annotations.NonNls) ResourceBundleEditorRenderer(com.intellij.lang.properties.editor.inspections.ResourceBundleEditorRenderer) ContainerUtil(com.intellij.util.containers.ContainerUtil) DeleteProvider(com.intellij.ide.DeleteProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) StringSelection(java.awt.datatransfer.StringSelection) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PsiNamedElement(com.intellij.psi.PsiNamedElement) SafeDeleteHandler(com.intellij.refactoring.safeDelete.SafeDeleteHandler) Logger(com.intellij.openapi.diagnostic.Logger) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) UsageTarget(com.intellij.usages.UsageTarget) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) UsageView(com.intellij.usages.UsageView) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) CopyProvider(com.intellij.ide.CopyProvider) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) ContextHelpAction(com.intellij.ide.actions.ContextHelpAction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) CopyPasteManager(com.intellij.openapi.ide.CopyPasteManager) CopyProvider(com.intellij.ide.CopyProvider) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) StringSelection(java.awt.datatransfer.StringSelection) IProperty(com.intellij.lang.properties.IProperty) ArrayList(java.util.ArrayList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Collection(java.util.Collection) ResourceBundle(com.intellij.lang.properties.ResourceBundle)

Example 15 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class GroovyLanguageInjectionSupport method removeInjectionInPlace.

@Override
public boolean removeInjectionInPlace(@Nullable final PsiLanguageInjectionHost psiElement) {
    if (!isStringLiteral(psiElement))
        return false;
    GrLiteralContainer host = (GrLiteralContainer) psiElement;
    final HashMap<BaseInjection, Pair<PsiMethod, Integer>> injectionsMap = ContainerUtil.newHashMap();
    final ArrayList<PsiElement> annotations = new ArrayList<>();
    final Project project = host.getProject();
    final Configuration configuration = Configuration.getProjectInstance(project);
    collectInjections(host, configuration, this, injectionsMap, annotations);
    if (injectionsMap.isEmpty() && annotations.isEmpty())
        return false;
    final ArrayList<BaseInjection> originalInjections = new ArrayList<>(injectionsMap.keySet());
    final List<BaseInjection> newInjections = ContainerUtil.mapNotNull(originalInjections, (NullableFunction<BaseInjection, BaseInjection>) injection -> {
        final Pair<PsiMethod, Integer> pair = injectionsMap.get(injection);
        final String placeText = JavaLanguageInjectionSupport.getPatternStringForJavaPlace(pair.first, pair.second);
        final BaseInjection newInjection = injection.copy();
        newInjection.setPlaceEnabled(placeText, false);
        return InjectorUtils.canBeRemoved(newInjection) ? null : newInjection;
    });
    configuration.replaceInjectionsWithUndo(project, newInjections, originalInjections, annotations);
    return true;
}
Also used : Language(com.intellij.lang.Language) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) IElementType(com.intellij.psi.tree.IElementType) GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) NonNls(org.jetbrains.annotations.NonNls) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) HashMap(java.util.HashMap) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) ArrayList(java.util.ArrayList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) TokenSets(org.jetbrains.plugins.groovy.lang.lexer.TokenSets) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrLiteralContainer(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteralContainer) AbstractLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) NullableFunction(com.intellij.util.NullableFunction) LightElement(com.intellij.psi.impl.light.LightElement) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) List(java.util.List) Configuration(org.intellij.plugins.intelliLang.Configuration) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) Processor(com.intellij.util.Processor) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions) Pair(com.intellij.openapi.util.Pair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) com.intellij.psi(com.intellij.psi) JavaLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.java.JavaLanguageInjectionSupport) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) NotNull(org.jetbrains.annotations.NotNull) GroovyPatterns(org.jetbrains.plugins.groovy.lang.psi.patterns.GroovyPatterns) Configuration(org.intellij.plugins.intelliLang.Configuration) ArrayList(java.util.ArrayList) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Project(com.intellij.openapi.project.Project) GrLiteralContainer(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteralContainer) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) Pair(com.intellij.openapi.util.Pair)

Aggregations

NullableFunction (com.intellij.util.NullableFunction)19 NotNull (org.jetbrains.annotations.NotNull)17 ContainerUtil (com.intellij.util.containers.ContainerUtil)15 Nullable (org.jetbrains.annotations.Nullable)13 List (java.util.List)12 Project (com.intellij.openapi.project.Project)11 StringUtil (com.intellij.openapi.util.text.StringUtil)7 Logger (com.intellij.openapi.diagnostic.Logger)6 java.util (java.util)6 Collections (java.util.Collections)6 Comparing (com.intellij.openapi.util.Comparing)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 com.intellij.psi (com.intellij.psi)5 java.awt (java.awt)5 javax.swing (javax.swing)5 ArrayList (java.util.ArrayList)4 Extensions (com.intellij.openapi.extensions.Extensions)3 Ref (com.intellij.openapi.util.Ref)3 PsiFile (com.intellij.psi.PsiFile)3 ActionEvent (java.awt.event.ActionEvent)3