Add statistics collector
This commit is contained in:
committed by
Igor Yakovlev
parent
63e687f67e
commit
e146d308db
+21
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.statistics
|
||||||
|
|
||||||
|
object AddValToDataClassParamCollector {
|
||||||
|
|
||||||
|
fun log(timeStarted: Long, timeFinished: Long, isValAdded: Boolean, onSymbol: Char, isBeforeTyping: Boolean) {
|
||||||
|
|
||||||
|
val data = mapOf(
|
||||||
|
"lagging" to (timeFinished - timeStarted).toString(),
|
||||||
|
"isValAdded" to isValAdded.toString(),
|
||||||
|
"onSymbol" to onSymbol.toString(),
|
||||||
|
"isBeforeTyping" to isBeforeTyping.toString()
|
||||||
|
)
|
||||||
|
|
||||||
|
KotlinFUSLogger.log(FUSEventGroups.Editor, "addValToDataClassParameters", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,10 +44,14 @@ import com.intellij.psi.util.PsiTreeUtil;
|
|||||||
import com.intellij.util.text.CharArrayUtil;
|
import com.intellij.util.text.CharArrayUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.KtNodeTypes;
|
import org.jetbrains.kotlin.KtNodeTypes;
|
||||||
|
import org.jetbrains.kotlin.idea.statistics.AddValToDataClassParamCollector;
|
||||||
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens;
|
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens;
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||||
import org.jetbrains.kotlin.psi.*;
|
import org.jetbrains.kotlin.psi.*;
|
||||||
|
|
||||||
|
import static java.lang.System.currentTimeMillis;
|
||||||
|
import static java.lang.System.runFinalization;
|
||||||
|
|
||||||
class KotlinTypedHandlerInner {
|
class KotlinTypedHandlerInner {
|
||||||
final static TokenSet CONTROL_FLOW_EXPRESSIONS = TokenSet.create(
|
final static TokenSet CONTROL_FLOW_EXPRESSIONS = TokenSet.create(
|
||||||
KtNodeTypes.IF,
|
KtNodeTypes.IF,
|
||||||
@@ -84,7 +88,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case ')':
|
case ')':
|
||||||
dataClassValParameterInsert(project, editor, file, /*beforeType = */ true);
|
dataClassValParameterInsert(project, editor, file, /*beforeType = */ true, c);
|
||||||
break;
|
break;
|
||||||
case '<':
|
case '<':
|
||||||
kotlinLTTyped = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET &&
|
kotlinLTTyped = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET &&
|
||||||
@@ -268,7 +272,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
return Result.STOP;
|
return Result.STOP;
|
||||||
}
|
}
|
||||||
else if (c == ',' || c == ')') {
|
else if (c == ',' || c == ')') {
|
||||||
dataClassValParameterInsert(project, editor, file, /*beforeType = */ false);
|
dataClassValParameterInsert(project, editor, file, /*beforeType = */ false, c);
|
||||||
}
|
}
|
||||||
else if (c == '{' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
|
else if (c == '{' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
|
||||||
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
||||||
@@ -335,44 +339,65 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
return Result.CONTINUE;
|
return Result.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void dataClassValParameterInsert(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, boolean beforeType) {
|
private static void dataClassValParameterInsert(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, boolean beforeType, char character) {
|
||||||
|
|
||||||
if (!KotlinEditorOptions.getInstance().isAutoAddValKeywordToDataClassParameters()) return;
|
if (!KotlinEditorOptions.getInstance().isAutoAddValKeywordToDataClassParameters()) return;
|
||||||
|
|
||||||
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
long timeStarted = currentTimeMillis();
|
||||||
|
boolean isValAdded = false;
|
||||||
|
boolean needCallFUS = false;
|
||||||
|
|
||||||
int commaOffset = editor.getCaretModel().getOffset();
|
try {
|
||||||
if (!beforeType) commaOffset--;
|
|
||||||
if (commaOffset < 1) return;
|
|
||||||
|
|
||||||
PsiElement elementOnCaret = file.findElementAt(commaOffset);
|
Document document = editor.getDocument();
|
||||||
if (elementOnCaret == null) return;
|
PsiDocumentManager.getInstance(project).commitDocument(document);
|
||||||
|
|
||||||
boolean contextMatched = false;
|
int commaOffset = editor.getCaretModel().getOffset();
|
||||||
PsiElement parentElement = elementOnCaret.getParent();
|
if (!beforeType) commaOffset--;
|
||||||
if (parentElement instanceof KtParameterList) {
|
if (commaOffset < 1) return;
|
||||||
parentElement = parentElement.getParent();
|
|
||||||
if (parentElement instanceof KtPrimaryConstructor) {
|
PsiElement elementOnCaret = file.findElementAt(commaOffset);
|
||||||
|
if (elementOnCaret == null) return;
|
||||||
|
|
||||||
|
boolean contextMatched = false;
|
||||||
|
PsiElement parentElement = elementOnCaret.getParent();
|
||||||
|
if (parentElement instanceof KtParameterList) {
|
||||||
parentElement = parentElement.getParent();
|
parentElement = parentElement.getParent();
|
||||||
if (parentElement instanceof KtClass) {
|
if (parentElement instanceof KtPrimaryConstructor) {
|
||||||
KtClass klassElement = ((KtClass)parentElement);
|
parentElement = parentElement.getParent();
|
||||||
contextMatched = klassElement.isData() || klassElement.hasModifier(KtTokens.INLINE_KEYWORD);
|
if (parentElement instanceof KtClass) {
|
||||||
|
KtClass klassElement = ((KtClass) parentElement);
|
||||||
|
contextMatched = klassElement.isData() || klassElement.hasModifier(KtTokens.INLINE_KEYWORD);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!contextMatched) return;
|
||||||
|
|
||||||
|
PsiElement leftElement = PsiTreeUtil.skipWhitespacesAndCommentsBackward(elementOnCaret);
|
||||||
|
|
||||||
|
if (!(leftElement instanceof KtParameter)) return;
|
||||||
|
|
||||||
|
KtParameter ktParameter = (KtParameter) leftElement;
|
||||||
|
KtTypeReference typeReference = ktParameter.getTypeReference();
|
||||||
|
if (typeReference == null) return;
|
||||||
|
|
||||||
|
if (ktParameter.hasValOrVar()) {
|
||||||
|
needCallFUS = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeReference.getTextLength() == 0) return;
|
||||||
|
|
||||||
|
document.insertString(leftElement.getTextOffset(), "val ");
|
||||||
|
isValAdded = true;
|
||||||
|
needCallFUS = true;
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (needCallFUS) {
|
||||||
|
long timeFinished = currentTimeMillis();
|
||||||
|
AddValToDataClassParamCollector.INSTANCE.log(timeStarted, timeFinished, isValAdded, character, beforeType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!contextMatched) return;
|
|
||||||
|
|
||||||
PsiElement leftElement = PsiTreeUtil.skipWhitespacesAndCommentsBackward(elementOnCaret);
|
|
||||||
|
|
||||||
if (!(leftElement instanceof KtParameter)) return;
|
|
||||||
|
|
||||||
KtParameter ktParameter = (KtParameter)leftElement;
|
|
||||||
if (ktParameter.hasValOrVar()) return;
|
|
||||||
KtTypeReference typeReference = ktParameter.getTypeReference();
|
|
||||||
if (typeReference == null) return;
|
|
||||||
if (typeReference.getTextLength() == 0) return;
|
|
||||||
|
|
||||||
editor.getDocument().insertString(leftElement.getTextOffset(), "val ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user