| /* |
| * Copyright 2000-2013 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.jetbrains.python.console; |
| |
| import com.intellij.execution.console.LanguageConsoleImpl; |
| import com.intellij.execution.ui.ConsoleViewContentType; |
| import com.intellij.openapi.application.ApplicationManager; |
| import com.intellij.openapi.editor.Editor; |
| import com.intellij.openapi.util.Key; |
| import com.intellij.openapi.util.text.StringUtil; |
| import com.intellij.openapi.vfs.VirtualFile; |
| import com.jetbrains.python.console.parsing.PythonConsoleData; |
| import org.jetbrains.annotations.NotNull; |
| import org.jetbrains.annotations.Nullable; |
| |
| /** |
| * @author traff |
| */ |
| public class PyConsoleUtil { |
| public static final String ORDINARY_PROMPT = ">>> "; |
| public static final String INPUT_PROMPT = ">? "; |
| public static final String INDENT_PROMPT = "... "; |
| static final String HELP_PROMPT = "help> "; |
| public static final String EXECUTING_PROMPT = ""; |
| |
| private static final String IPYTHON_PAGING_PROMPT = "---Return to continue, q to quit---"; |
| |
| static final String[] PROMPTS = new String[]{ |
| ORDINARY_PROMPT, |
| INDENT_PROMPT, |
| HELP_PROMPT, |
| IPYTHON_PAGING_PROMPT |
| }; |
| public static final String DOUBLE_QUOTE_MULTILINE = "\"\"\""; |
| public static final String SINGLE_QUOTE_MULTILINE = "'''"; |
| |
| static final Key<PythonConsoleData> PYTHON_CONSOLE_DATA = Key.create("python-console-data"); |
| |
| private PyConsoleUtil() { |
| } |
| |
| public static boolean isPagingPrompt(@Nullable String prompt) { |
| return prompt != null && IPYTHON_PAGING_PROMPT.equals(prompt.trim()); |
| } |
| |
| static String processPrompts(final LanguageConsoleImpl languageConsole, String string) { |
| // Change prompt |
| for (String prompt : PROMPTS) { |
| if (string.startsWith(prompt)) { |
| // Process multi prompts here |
| if (prompt != HELP_PROMPT) { |
| final StringBuilder builder = new StringBuilder(); |
| builder.append(prompt).append(prompt); |
| while (string.startsWith(builder.toString())) { |
| builder.append(prompt); |
| } |
| final String multiPrompt = builder.toString().substring(prompt.length()); |
| if (prompt == INDENT_PROMPT) { |
| prompt = multiPrompt; |
| } |
| string = string.substring(multiPrompt.length()); |
| } |
| else { |
| string = string.substring(prompt.length()); |
| } |
| |
| // Change console editor prompt if required |
| final String currentPrompt = languageConsole.getPrompt(); |
| final String trimmedPrompt = prompt.trim(); |
| if (currentPrompt != null && !currentPrompt.equals(trimmedPrompt)) { |
| languageConsole.setPrompt(trimmedPrompt); |
| scrollDown(languageConsole.getConsoleEditor()); |
| } |
| break; |
| } |
| } |
| return string; |
| } |
| |
| public static boolean isMultilineStarts(String line, String substring) { |
| return StringUtil.getOccurrenceCount(line, substring) % 2 == 1; |
| } |
| |
| public static void scrollDown(final Editor currentEditor) { |
| ApplicationManager.getApplication().invokeLater(new Runnable() { |
| @Override |
| public void run() { |
| currentEditor.getCaretModel().moveToOffset(currentEditor.getDocument().getTextLength()); |
| } |
| }); |
| } |
| |
| public static boolean isSingleQuoteMultilineStarts(String line) { |
| return isMultilineStarts(line, SINGLE_QUOTE_MULTILINE); |
| } |
| |
| public static boolean isDoubleQuoteMultilineStarts(String line) { |
| return isMultilineStarts(line, DOUBLE_QUOTE_MULTILINE); |
| } |
| |
| public static boolean detectIPythonImported(@NotNull String text, final ConsoleViewContentType outputType) { |
| return text.contains("PyDev console: using IPython ") && outputType == ConsoleViewContentType.ERROR_OUTPUT; |
| } |
| |
| public static boolean detectSourcePrinting(@NotNull String text) { |
| return text.contains("Source:"); |
| } |
| |
| public static boolean detectIPythonStart(@NotNull String text) { |
| return text.contains("IPython-->"); |
| } |
| |
| public static boolean detectIPythonEnd(@NotNull String text) { |
| return text.contains("<--IPython"); |
| } |
| |
| public static boolean detectIPythonAutomagicOn(@NotNull String text) { |
| return text.contains("Automagic is ON, % prefix NOT needed for magic functions."); |
| } |
| |
| public static boolean detectIPythonAutomagicOff(@NotNull String text) { |
| return text.contains("Automagic is OFF, % prefix IS needed for magic functions."); |
| } |
| |
| public static void markIPython(@NotNull VirtualFile file) { |
| PythonConsoleData consoleData = getOrCreateIPythonData(file); |
| consoleData.setIPythonEnabled(true); |
| } |
| |
| @NotNull |
| public static PythonConsoleData getOrCreateIPythonData(@NotNull VirtualFile file) { |
| PythonConsoleData consoleData = file.getUserData(PYTHON_CONSOLE_DATA); |
| if (consoleData == null) { |
| consoleData = new PythonConsoleData(); |
| file.putUserData(PYTHON_CONSOLE_DATA, consoleData); |
| } |
| return consoleData; |
| } |
| |
| public static void setIPythonAutomagic(@NotNull VirtualFile file, boolean detected) { |
| PythonConsoleData consoleData = getOrCreateIPythonData(file); |
| consoleData.setIPythonAutomagic(detected); |
| } |
| |
| public static void setCurrentIndentSize(@NotNull VirtualFile file, int indentSize) { |
| PythonConsoleData consoleData = getOrCreateIPythonData(file); |
| consoleData.setIndentSize(indentSize); |
| } |
| } |
| |
| |