IntelliJ does not sort Kotlin imports according to ktlint's expectations - kotlin

When I select Code → Optimize Imports or Code → Reformat Code, the IntelliJ does optimize and sort imports, but even though I am using code style settings from Kotlin code style, the imports are not sorted in lexicographic order (not entirely at least). For example, this is the output produced:
import com.fasterxml.jackson.databind.ObjectMapper
import io.dropwizard.jackson.Jackson
import io.kotlintest.assertSoftly
import io.kotlintest.matchers.types.shouldBeNull
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.intellij.lang.annotations.Language
import java.time.Instant // This should not be at the bottom!!!
while this is what expected by ktlint:
import com.fasterxml.jackson.databind.ObjectMapper
import io.dropwizard.jackson.Jackson
import io.kotlintest.assertSoftly
import io.kotlintest.matchers.types.shouldBeNull
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import java.time.Instant // should be here instead
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.intellij.lang.annotations.Language
In other words, it seems the formatter always puts imports from java.* package at the bottom no matter what. Is there a way to make it be in line with what ktlint expects (and what would actually constitute lexicographic order)? Any hidden option I am missing or something?
I am using IntelliJ IDEA 2019.3.1 (Ultimate Edition) with the Kotlin plugin version 1.3.61-release-IJ2019.3-1. Version of ktlint is 0.36.0

This is a bug/missing functionality in the Kotlin IDEA plugin: https://youtrack.jetbrains.com/issue/KT-10974. Please vote.

You can change IntelliJ's import order for Kotlin to satisfy ktlint:
In Settings > Editor > Code Style > Kotlin you can change Import Layout by removing everything except import all other imports from the list:
Before:
After:

What worked for me is manually removing all imports and then reimporting each import.

Related

How to import ErrorUtils in React Native

RN version: 0.50
Testing on Android, haven't tested on iOS
I am trying to use ErrorUtils.setGlobalHandler as described in this github issue: https://github.com/facebook/react-native/issues/1194
However, what is not clear from the issue is how to import ErrorUtils. It's not in the react documentation: https://facebook.github.io/react-native/docs/0.50/getting-started.html
Previously, in RN 0.41, I was able to import ErrorUtils with import ErrorUtils from "ErrorUtils"; However, in 0.50 I am getting a red react popup with the following message when I try to import ErrorUtils like this:
com.facebook.react.common.JavascriptException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' failed to load.
I've also tried import { ErrorUtils } from 'react-native'; but it doesn't seem to exist there. The error is:
Cannot read property 'setGlobalHandler' of undefined
How do I properly import ErrorUtils in RN 0.50?
ErrorUtils is a global variable, therfore it doesn't need to be imported. You can verify this with console.log(global.ErrorUtils)
However it is exported as module anyways (here). The comment there also has more information why it is done this way.
You can import the module like this:
import ErrorUtils from 'ErrorUtils';
For anyone on RN61+, you should no longer import the module as you will experience the following error in the running metro bundler:
Error: Unable to resolve module `ErrorUtils`
Instead, just use the module without importing as this is a global variable, as stated by leo
I did created a global.d.ts to define a global variable,
interface Global {
ErrorUtils: {
setGlobalHandler: any
reportFatalError: any
getGlobalHandler: any
}
}
declare var global: Global
then, at where you are trying to use it, simply
global.ErrorUtils.setGlobalHandler(xxxx)

Import FilterLeafReader pylucene 4.9

I use PyLucene 4.9.0-0 for a school project and I need to use FilterLeafReader but I can't figure out how to import this class.
I tried from org.apache.lucene.search import FilterLeafReader but i receive an import error.
FilterLeafReader does not exist in Lucene 4.9, and so is not available in PyLucene 4.9.
AtomicReader classes were renamed LeafReader in 5.0, so you should be using FilterAtomicReader instead. Also, in either case, it's in org.apache.lucene.index, not org.apache.lucene.search

jpod error, where is the FileLocator?

I am trying to evaluate the jPod PDF library, the import line:
import de.intarsys.tools.locator.FileLocator;
says it cannot be resolved. I have jPod.JAR and iscommon.jar, What other JARs do I need to get the FileLocator?

IntelliJ does not sort Kotlin imports

When writing Java code, IntelliJ automatically sorts imports by name. However, when importin members in Kotlin, they remain unsorted. Selecting Code → Optimize Imports (Ctrl+Alt+O) does nothing.
Here is an example:
import kotlin.platform.platformStatic
import java.text.DateFormaty
import org.hibernate.validator.constraints.NotEmpty as notEmpty
import com.fasterxml.jackson.annotation.JsonProperty as jsonProperty
import javax.validation.constraints.NotNull as notNull
import javax.validation.Valid as valid
What I'm expecting:
import com.fasterxml.jackson.annotation.JsonProperty as jsonProperty
import org.hibernate.validator.constraints.NotEmpty as notEmpty
import kotlin.platform.platformStatic
import java.text.DateFormat
import javax.validation.constraints.NotNull as notNull
import javax.validation.Valid as valid
I'm using IntelliJ 14.0.2 with the Kotlin plugin (version 0.10.195)
This has been fixed in recent versions of IntelliJ, for example the current version (at time of writing 2017.3.3)
If you only have a perpetual fallback licence for the full version of IntelliJ 14 - you could still use the community edition of a more recent version.

Why can't I access Jython access stdlib modules when called from tomcat?

I can take an application with the following code in it:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import os");
interpreter.exec("import mylib");
Where the following is resources/Lib/mylib/__init__.py:
from __future__ import print_function
from . import myfriend as thing
import os
print("Yep, everything works")
and compile it using maven, producing a my-app-with-dependencies.jar
I can easily run it with java -jar my-app-with-depenendencies.jar and it works just fine, hooray!
Here's where the sad part comes in. I can put this exact same code inside a Spring handler:
#RequestMapping("/doesnotwork")
public #ResponseBody String sadness() {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import os");
interpreter.exec("import mylib");
return "Quoth the Java, nevermore";
}
and magically this no longer works. Not one little bit.
I can however move my file from resources/Lib/ to webapp/WEB-INF/lib/Lib/ and import mylib works. But within mylib I can no longer import from __future__ or os. I can import sys, and my sys.path looks like this:
['/path/to/my/webapp/WEB-INF/lib/Lib', '__classpath__', '__pyclasspath__/']
My sys.path_importer_cache looks like this:
{'__classpath__': <type 'org.python.core.JavaImporter'>,
'/path/to/my/webapp/WEB-INF/lib/Lib': None,
'/path/to/my/webapp/WEB-INF/lib/Lib/mylib': None,
'__pyclasspath__/': <ClasspathPyImporter object at 0x2>}
What am I doing wrong that I can't import the stdlib? /path/to/my/webapp/WEB-INF/lib contains both jython-2.7-b1.jar and jython-standalone-2.7-b1.jar. I've even tried inserting those jar files into my path and still no dice.
I can import java classes from .jar files present in the folder, except for ones found in the jython .jars. For instance, inside jython-2.7-b1.jar are org/python/apache/xml/serialize/Serializer.class. I can import org.python but there only exists org.python.__name__.