IDEA-CR-5434 don't add extension functions related to other subsystem to commonly used types

This commit is contained in:
Vladimir Krivosheev
2015-10-06 17:56:36 +02:00
parent b97b6d4b3c
commit fbd447f5ce
6 changed files with 50 additions and 22 deletions
@@ -16,7 +16,9 @@
package org.jetbrains.builtInWebServer
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.parentPath
import com.intellij.openapi.util.io.endsWithName
import com.intellij.openapi.util.io.endsWithSlash
import com.intellij.openapi.util.io.getParentPath
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.util.PathUtilRt
import io.netty.channel.ChannelHandlerContext
@@ -51,7 +53,7 @@ private class DefaultWebServerPathHandler : WebServerPathHandler() {
var virtualFile = pathInfo.file
val isDirectory = if (virtualFile == null) pathInfo.ioFile!!.isDirectory else virtualFile.isDirectory
if (isDirectory) {
if (!WebServerPathHandler.endsWithSlash(decodedRawPath)) {
if (!endsWithSlash(decodedRawPath)) {
WebServerPathHandler.redirectToDirectory(request, channel, if (isCustomHost) path else (projectName + '/' + path))
return true
}
@@ -71,13 +73,14 @@ private class DefaultWebServerPathHandler : WebServerPathHandler() {
pathToFileManager.pathToInfoCache.put(path, pathInfo)
}
else if (!path.endsWith(pathInfo.name)) {
if (WebServerPathHandler.endsWithSlash(decodedRawPath)) {
else if (!endsWithName(path, pathInfo.name)) {
if (endsWithSlash(decodedRawPath)) {
indexUsed = true
}
else {
// FallbackResource feature in action, /login requested, /index.php retrieved, we must not redirect /login to /login/
if (path.endsWith(PathUtilRt.getFileName(pathInfo.path.parentPath!!))) {
val parentPath = getParentPath(pathInfo.path)
if (parentPath != null && endsWithName(path, PathUtilRt.getFileName(parentPath))) {
WebServerPathHandler.redirectToDirectory(request, channel, if (isCustomHost) path else ("$projectName/$path"))
return true
}
@@ -51,8 +51,4 @@ public abstract class WebServerPathHandler {
response.headers().add(HttpHeaderNames.LOCATION, url.toASCIIString());
Responses.send(response, channel, request);
}
protected static boolean endsWithSlash(@NotNull String path) {
return path.charAt(path.length() - 1) == '/';
}
}
@@ -30,7 +30,7 @@ import com.intellij.openapi.ui.Messages
import com.intellij.openapi.updateSettings.impl.UpdateSettings
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.io.FileUtilRt
import com.intellij.openapi.util.io.parentPath
import com.intellij.openapi.util.io.getParentPath
import com.intellij.util.Consumer
import gnu.trove.THashSet
import java.io.File
@@ -132,7 +132,7 @@ fun getPaths(input: InputStream): Set<String> {
var path = entry.name
result.add(path)
while (true) {
path = path.parentPath ?: break
path = getParentPath(path) ?: break
result.add("$path/")
}
}
@@ -15,6 +15,8 @@
*/
package com.intellij.openapi.util.io
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.PathUtilRt
import java.io.File
val File.systemIndependentPath: String
@@ -23,14 +25,9 @@ val File.systemIndependentPath: String
val File.parentSystemIndependentPath: String
get() = getParent().replace(File.separatorChar, '/')
val String.parentPath: String?
get() {
if (isEmpty()) {
return null
}
var end = Math.max(lastIndexOf('/'), lastIndexOf('\\'))
if (end == length() - 1) {
end = Math.max(lastIndexOf('/', end - 1), lastIndexOf('\\', end - 1))
}
return if (end == -1) null else substring(0, end)
}
// PathUtilRt.getParentPath returns empty string if no parent path, but in Kotlin "null" is better because elvis operator could be used
fun getParentPath(path: String) = StringUtil.nullize(PathUtilRt.getParentPath(path))
fun endsWithSlash(path: String) = path.getOrNull(path.length() - 1) == '/'
fun endsWithName(path: String, name: String) = path.endsWith(name) && (path.length() == name.length() || path.getOrNull(path.length() - name.length() - 1) == '/')
@@ -27,5 +27,6 @@
<orderEntry type="library" name="gson" level="project" />
<orderEntry type="library" scope="TEST" name="KotlinJavaRuntime" level="project" />
<orderEntry type="module" module-name="util-tests" scope="TEST" />
<orderEntry type="library" scope="TEST" name="assertJ" level="project" />
</component>
</module>
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.util.io
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
private class FileUtilKtTest {
@Test fun testEndsWithName() {
assertThat(endsWithName("foo", "bar")).isFalse()
assertThat(endsWithName("foo", "foo")).isTrue()
assertThat(endsWithName("foo/bar", "foo")).isFalse()
assertThat(endsWithName("foo/bar", "bar")).isTrue()
assertThat(endsWithName("/foo", "foo")).isTrue()
assertThat(endsWithName("fooBar", "Bar")).isFalse()
assertThat(endsWithName("/foo/bar_bar", "bar")).isFalse()
}
}