MCPcopy Index your code
hub / github.com/ipython/ipython / _is_in_string_or_comment

Method _is_in_string_or_comment

IPython/core/completer.py:2653–2767  ·  view source on GitHub ↗

Determine if the cursor is inside a string or comment. Returns (is_string, is_in_expression) tuple: - is_string: True if in any kind of string - is_in_expression: True if inside an f-string/t-string expression

(self, text)

Source from the content-addressed store, hash-verified

2651 return False
2652
2653 def _is_in_string_or_comment(self, text):
2654 """
2655 Determine if the cursor is inside a string or comment.
2656 Returns (is_string, is_in_expression) tuple:
2657 - is_string: True if in any kind of string
2658 - is_in_expression: True if inside an f-string/t-string expression
2659 """
2660 in_single_quote = False
2661 in_double_quote = False
2662 in_triple_single = False
2663 in_triple_double = False
2664 in_template_string = False # Covers both f-strings and t-strings
2665 in_expression = False # For expressions in f/t-strings
2666 expression_depth = 0 # Track nested braces in expressions
2667 i = 0
2668
2669 while i < len(text):
2670 # Check for f-string or t-string start
2671 if (
2672 i + 1 < len(text)
2673 and text[i] in ("f", "t")
2674 and (text[i + 1] == '"' or text[i + 1] == "'")
2675 and not (
2676 in_single_quote
2677 or in_double_quote
2678 or in_triple_single
2679 or in_triple_double
2680 )
2681 ):
2682 in_template_string = True
2683 i += 1 # Skip the 'f' or 't'
2684
2685 # Handle triple quotes
2686 if i + 2 < len(text):
2687 if (
2688 text[i : i + 3] == '"""'
2689 and not in_single_quote
2690 and not in_triple_single
2691 ):
2692 in_triple_double = not in_triple_double
2693 if not in_triple_double:
2694 in_template_string = False
2695 i += 3
2696 continue
2697 if (
2698 text[i : i + 3] == "'''"
2699 and not in_double_quote
2700 and not in_triple_double
2701 ):
2702 in_triple_single = not in_triple_single
2703 if not in_triple_single:
2704 in_template_string = False
2705 i += 3
2706 continue
2707
2708 # Handle escapes
2709 if text[i] == "\\" and i + 1 < len(text):
2710 i += 2

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected