Skip to content

Instantly share code, notes, and snippets.

fun <T> Iterable<Iterable<T>>.flatten() = object: Iterable<T> {
val collection = this@flatten.Ifilter { it.isEmpty() }.iterator()
var element = collection.nextOrNull()?.iterator()
override fun iterator() = iterate {
if (element.hasNext())
return@iterate element!!.next()
else if (collection.hasNext()) {
element = collection.next().iterator()
return@iterate element!!.next()
}
public static String getS(){
double z;
int x = 5;
System.out.print("Hello!" + 2);
x = x*22 - 4;
double y = Math.acos(x * Math.random());
System.out.println("X=" + x);
System.out.print("Hello!" + 2);
x = x*22 - 4;
System.out.println("X=" + x);
var leftPsi: PsiElement? = clone.firstPsi
val result = generateSequence (clone.firstPsi) { it.findNextSibling(end) }
.filter { ! it.haveSibling(end) }
.map {
val result = leftPsi!! to it
leftPsi = it.findNextSibling(end)
result
}
/*
* Copyright 2002-2017 the original author or authors.
*
* 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
@suhininalex
suhininalex / LiteralPathElement.java
Last active August 1, 2017 15:34
After refactoring.
/*
* Copyright 2002-2017 the original author or authors.
*
* 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
@suhininalex
suhininalex / TokenExtender.kt
Created September 25, 2017 10:24
mapNotNull (infer error)
package com.suhininalex.clones.index
import com.intellij.psi.PsiElement
import com.suhininalex.clones.core.utils.isNoiseElement
import com.suhininalex.clones.core.utils.nextLeafElement
import com.suhininalex.clones.core.utils.prevLeafElement
fun PsiElement.nextElements(): Sequence<PsiElement> =
generateSequence (this) { it.nextLeafElement() }
infix fun Int.clip(range: IntRange): Int {
return when {
this < range.start -> range.start
this > range.endInclusive -> range.endInclusive
else -> this
}
}
fun FileTextRange.intersects(other: FileTextRange, gap: Int = 0): Boolean {
return when {
file != other.file -> false
range intersects other.range -> false
else -> extendLines(1).range intersects other.range
}
}
fun findTextRanges(project: Project, hash: HashValue): List<FileTextRange> {
return FileBasedIndex.getInstance()
.getValuesWithFiles(NAME, hash, GlobalSearchScope.projectScope(project))
.flatMap { (file, ranges) -> ranges.map { FileTextRange(file, it) } }
}
private fun process(content: FileContent): Map<HashValue, TextRanges> {
Logger.log("[Indexing] ${content.psiFile.name}")
return content.psiFile
.findIndexableElements()
.flatMap { it.createHashFragments(content.file) }
.groupBy { it.hash }
.mapValues { (_, values) -> values.map { it.range } }
}