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();
}
}
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);
}
}
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()]);
}
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);
}
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;
}
Aggregations