Move Maybe class and its inheritors to util package
This commit is contained in:
+10
-11
@@ -30,10 +30,9 @@ import org.jetbrains.jet.lang.psi.ValueArgument
|
|||||||
import org.jetbrains.jet.plugin.JetBundle
|
import org.jetbrains.jet.plugin.JetBundle
|
||||||
import org.jetbrains.jet.lang.resolve.calls.model.DefaultValueArgument
|
import org.jetbrains.jet.lang.resolve.calls.model.DefaultValueArgument
|
||||||
import org.jetbrains.jet.lang.resolve.calls.model.VarargValueArgument
|
import org.jetbrains.jet.lang.resolve.calls.model.VarargValueArgument
|
||||||
|
import org.jetbrains.jet.plugin.util.Maybe
|
||||||
abstract class Maybe<out V, out E>
|
import org.jetbrains.jet.plugin.util.MaybeError
|
||||||
public class Value<V, E>(public val value: V) : Maybe<V, E>()
|
import org.jetbrains.jet.plugin.util.MaybeValue
|
||||||
public class Error<V, E>(public val error: E) : Maybe<V, E>()
|
|
||||||
|
|
||||||
// Internal because you shouldn't construct this manually. You can end up with an inconsistant CallDescription.
|
// Internal because you shouldn't construct this manually. You can end up with an inconsistant CallDescription.
|
||||||
public class CallDescription internal (
|
public class CallDescription internal (
|
||||||
@@ -56,13 +55,13 @@ public class CallDescription internal (
|
|||||||
public fun getPositionalArguments(): Maybe<List<ValueArgument>, String> {
|
public fun getPositionalArguments(): Maybe<List<ValueArgument>, String> {
|
||||||
|
|
||||||
val resolvedValueArguments = resolved.getValueArgumentsByIndex()
|
val resolvedValueArguments = resolved.getValueArgumentsByIndex()
|
||||||
?: return Error("duplicate.or.missing.arguments")
|
?: return MaybeError("duplicate.or.missing.arguments")
|
||||||
|
|
||||||
// Check for mixed default and passed arguments and return the passed parameters (or fail)
|
// Check for mixed default and passed arguments and return the passed parameters (or fail)
|
||||||
val indexOfFirstDefaultArgument = resolvedValueArguments.indexOf(DefaultValueArgument.DEFAULT)
|
val indexOfFirstDefaultArgument = resolvedValueArguments.indexOf(DefaultValueArgument.DEFAULT)
|
||||||
val valueArgumentGroups = if (indexOfFirstDefaultArgument >= 0) {
|
val valueArgumentGroups = if (indexOfFirstDefaultArgument >= 0) {
|
||||||
if (resolvedValueArguments.listIterator(indexOfFirstDefaultArgument).any { it != DefaultValueArgument.DEFAULT }) {
|
if (resolvedValueArguments.listIterator(indexOfFirstDefaultArgument).any { it != DefaultValueArgument.DEFAULT }) {
|
||||||
return Error("skipped.defaults")
|
return MaybeError("skipped.defaults")
|
||||||
}
|
}
|
||||||
resolvedValueArguments.subList(0, indexOfFirstDefaultArgument)
|
resolvedValueArguments.subList(0, indexOfFirstDefaultArgument)
|
||||||
} else {
|
} else {
|
||||||
@@ -73,17 +72,17 @@ public class CallDescription internal (
|
|||||||
if (valueArgumentGroups.size > 0) {
|
if (valueArgumentGroups.size > 0) {
|
||||||
val vararg = valueArgumentGroups.find { it is VarargValueArgument }
|
val vararg = valueArgumentGroups.find { it is VarargValueArgument }
|
||||||
if (vararg != null && vararg != valueArgumentGroups.last) {
|
if (vararg != null && vararg != valueArgumentGroups.last) {
|
||||||
return Error("vararg.not.last")
|
return MaybeError("vararg.not.last")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val valueArguments = valueArgumentGroups.flatMap { it.getArguments() }
|
val valueArguments = valueArgumentGroups.flatMap { it.getArguments() }
|
||||||
if (valueArguments.size < argumentCount) {
|
if (valueArguments.size < argumentCount) {
|
||||||
// Only happens if invalid arguments were thrown away.
|
// Only happens if invalid arguments were thrown away.
|
||||||
return Error("invalid.arguments")
|
return MaybeError("invalid.arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
return Value(valueArguments)
|
return MaybeValue(valueArguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,8 +128,8 @@ public abstract class AttributeCallReplacementIntention(name: String) : JetSelfT
|
|||||||
|
|
||||||
protected fun handleErrors<V: Any>(editor: Editor, maybeValue: Maybe<V, String>): V? {
|
protected fun handleErrors<V: Any>(editor: Editor, maybeValue: Maybe<V, String>): V? {
|
||||||
return when (maybeValue) {
|
return when (maybeValue) {
|
||||||
is Value -> maybeValue.value
|
is MaybeValue -> maybeValue.value
|
||||||
is Error -> {
|
is MaybeError -> {
|
||||||
intentionFailed(editor, maybeValue.error)
|
intentionFailed(editor, maybeValue.error)
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2014 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 org.jetbrains.jet.plugin.util
|
||||||
|
|
||||||
|
abstract class Maybe<out V, out E>
|
||||||
|
public class MaybeValue<V, E>(public val value: V) : Maybe<V, E>()
|
||||||
|
public class MaybeError<V, E>(public val error: E) : Maybe<V, E>()
|
||||||
|
|
||||||
Reference in New Issue
Block a user