2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.debugger.engine.evaluation;
18 import com.intellij.debugger.ui.DebuggerEditorImpl;
19 import com.intellij.lang.LanguageUtil;
20 import com.intellij.openapi.fileTypes.FileType;
21 import com.intellij.openapi.fileTypes.FileTypeManager;
22 import com.intellij.openapi.fileTypes.StdFileTypes;
23 import com.intellij.openapi.util.Comparing;
24 import com.intellij.openapi.util.Trinity;
25 import com.intellij.openapi.util.text.StringUtil;
26 import com.intellij.psi.*;
27 import com.intellij.xdebugger.XExpression;
28 import com.intellij.xdebugger.evaluation.EvaluationMode;
29 import com.intellij.xdebugger.impl.breakpoints.XExpressionImpl;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
33 public final class TextWithImportsImpl implements TextWithImports{
35 private final CodeFragmentKind myKind;
36 private String myText;
37 private final FileType myFileType;
38 private final String myImports;
40 public TextWithImportsImpl(@NotNull PsiElement expression) {
41 myKind = CodeFragmentKind.EXPRESSION;
42 final String text = expression.getText();
43 PsiFile containingFile = expression.getContainingFile();
44 if(containingFile instanceof PsiExpressionCodeFragment) {
46 myImports = ((JavaCodeFragment)containingFile).importsToString();
47 myFileType = StdFileTypes.JAVA;
50 Trinity<String, String, FileType> trinity = parseExternalForm(text);
51 myText = trinity.first;
52 myImports = trinity.second;
53 myFileType = trinity.third;
57 public TextWithImportsImpl (CodeFragmentKind kind, @NotNull String text, @NotNull String imports, @Nullable FileType fileType) {
61 myFileType = fileType;
64 public TextWithImportsImpl(CodeFragmentKind kind, @NotNull String text) {
66 Trinity<String, String, FileType> trinity = parseExternalForm(text);
67 myText = trinity.first;
68 myImports = trinity.second;
69 myFileType = trinity.third;
72 private static Trinity<String, String, FileType> parseExternalForm(String s) {
73 String[] split = s.split(String.valueOf(DebuggerEditorImpl.SEPARATOR));
74 return Trinity.create(split[0], split.length > 1 ? split[1] : "", split.length > 2 ? FileTypeManager.getInstance().getStdFileType(split[2]) : null);
78 public CodeFragmentKind getKind() {
83 public String getText() {
88 public @NotNull String getImports() {
92 public boolean equals(Object object) {
93 if(!(object instanceof TextWithImportsImpl)) {
96 TextWithImportsImpl item = ((TextWithImportsImpl)object);
97 return Comparing.equal(item.myText, myText) && Comparing.equal(item.myImports, myImports);
100 public String toString() {
105 public String toExternalForm() {
106 String result = myText;
107 if (StringUtil.isNotEmpty(myImports) || myFileType != null) {
108 result += DebuggerEditorImpl.SEPARATOR + myImports;
110 if (myFileType != null) {
111 result += DebuggerEditorImpl.SEPARATOR + myFileType.getName();
116 public int hashCode() {
117 return myText.hashCode();
121 public boolean isEmpty() {
122 return StringUtil.isEmptyOrSpaces(getText());
126 public void setText(String newText) {
131 public FileType getFileType() {
136 public static XExpression toXExpression(@Nullable TextWithImports text) {
137 if (text != null && !text.getText().isEmpty()) {
138 return new XExpressionImpl(text.getText(),
139 LanguageUtil.getFileTypeLanguage(text.getFileType()),
140 StringUtil.nullize(text.getImports()),
141 getMode(text.getKind()));
146 private static EvaluationMode getMode(CodeFragmentKind kind) {
148 case EXPRESSION: return EvaluationMode.EXPRESSION;
149 case CODE_BLOCK: return EvaluationMode.CODE_FRAGMENT;
151 throw new IllegalStateException("Unknown kind " + kind);
154 private static CodeFragmentKind getKind(EvaluationMode mode) {
156 case EXPRESSION: return CodeFragmentKind.EXPRESSION;
157 case CODE_FRAGMENT: return CodeFragmentKind.CODE_BLOCK;
159 throw new IllegalStateException("Unknown mode " + mode);
162 public static TextWithImports fromXExpression(@Nullable XExpression expression) {
163 if (expression == null) return null;
165 if (expression.getCustomInfo() == null && expression.getLanguage() == null) {
166 return new TextWithImportsImpl(getKind(expression.getMode()), expression.getExpression());
169 return new TextWithImportsImpl(getKind(expression.getMode()),
170 expression.getExpression(),
171 StringUtil.notNullize(expression.getCustomInfo()),
172 LanguageUtil.getLanguageFileType(expression.getLanguage()));