| 1 | # LSP Test 4: Go to Definition (F12 or Ctrl+Click) |
| 2 | # ================================================= |
| 3 | # |
| 4 | # Instructions: |
| 5 | # 1. Place cursor on a symbol (function, class, variable) |
| 6 | # 2. Press F12 to jump to its definition |
| 7 | # 3. This works for: |
| 8 | # - Functions defined in this file |
| 9 | # - Classes defined in this file |
| 10 | # - Imported modules/functions |
| 11 | # - Variables (jumps to assignment) |
| 12 | # |
| 13 | # Try going to definition on the marked symbols: |
| 14 | |
| 15 | |
| 16 | def helper_function(x: int) -> int: |
| 17 | """A helper function defined at the top of the file.""" |
| 18 | return x * 2 |
| 19 | |
| 20 | |
| 21 | class DataProcessor: |
| 22 | """A class for processing data.""" |
| 23 | |
| 24 | def __init__(self, data: list): |
| 25 | self.data = data |
| 26 | |
| 27 | def process(self) -> list: |
| 28 | """Process the data and return results.""" |
| 29 | return [self.transform(item) for item in self.data] |
| 30 | |
| 31 | def transform(self, item): |
| 32 | """Transform a single item.""" |
| 33 | return item * 2 |
| 34 | |
| 35 | |
| 36 | # Test 1: Go to function definition |
| 37 | # Place cursor on "helper_function" and press F12 |
| 38 | result = helper_function(42) # <- F12 on helper_function |
| 39 | |
| 40 | # Test 2: Go to class definition |
| 41 | # Place cursor on "DataProcessor" and press F12 |
| 42 | processor = DataProcessor([1, 2, 3]) # <- F12 on DataProcessor |
| 43 | |
| 44 | # Test 3: Go to method definition |
| 45 | # Place cursor on "process" and press F12 |
| 46 | output = processor.process() # <- F12 on process |
| 47 | |
| 48 | # Test 4: Go to variable definition |
| 49 | # Place cursor on "result" below and press F12 |
| 50 | print(result) # <- F12 on result (should go to line 31) |
| 51 | |
| 52 | # Test 5: Go to imported function definition |
| 53 | # Place cursor on "path" and press F12 |
| 54 | from os import path |
| 55 | exists = path.exists("/tmp") # <- F12 on path or exists |
| 56 | |
| 57 | # Test 6: Go to standard library |
| 58 | # Place cursor on "print" and press F12 (may open stdlib) |
| 59 | print("hello") # <- F12 on print |
| 60 | |
| 61 | |
| 62 | # Nested definitions for testing |
| 63 | def outer_function(): |
| 64 | """Outer function containing nested definitions.""" |
| 65 | |
| 66 | def inner_function(): |
| 67 | """Inner function.""" |
| 68 | return "inner" |
| 69 | |
| 70 | return inner_function() |
| 71 | |
| 72 | |
| 73 | # Test 7: Go to nested function |
| 74 | nested_result = outer_function() # <- F12 on outer_function |