|
| 1 | +function path = resolvePath (name, contextFile) |
| 2 | + % RESOLVEPATH Attempt to determine the file which most likely contains |
| 3 | + % the provided identifier from the perspective of the context file. |
| 4 | + % |
| 5 | + % For example, if file "foo.m" refers to "ClassName", this returns the |
| 6 | + % file most likely containing the definition of "ClassName", or an |
| 7 | + % empty string if that file could not be determined. |
| 8 | + |
| 9 | + elementName = name; |
| 10 | + |
| 11 | + % The given identifier may be a reference within a class (e.g. 'obj.Prop') |
| 12 | + removeDot = count(name, '.') == 1; |
| 13 | + if removeDot |
| 14 | + nameResolver = matlab.internal.language.introspective.resolveName(name); |
| 15 | + if isempty(nameResolver.classInfo) |
| 16 | + elementName = extractAfter(name, '.'); |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + % Check for targets within the context file |
| 21 | + if isvarname(elementName) || iskeyword(elementName) |
| 22 | + nameResolver = matlab.internal.language.introspective.resolveName(contextFile); |
| 23 | + if ~isempty(nameResolver.classInfo) && (nameResolver.classInfo.isClass || nameResolver.classInfo.isMethod) |
| 24 | + classInfo = nameResolver.classInfo; |
| 25 | + className = matlab.internal.language.introspective.makePackagedName(classInfo.packageName, classInfo.className); |
| 26 | + targetName = append(className, '.', elementName); |
| 27 | + nameResolver = matlab.internal.language.introspective.resolveName(targetName); |
| 28 | + if nameResolver.isResolved |
| 29 | + if nameResolver.isCaseSensitive || isempty(matlab.internal.language.introspective.safeWhich(name, true)) |
| 30 | + % Target not found within file. Broaden search (e.g. look in base classes) |
| 31 | + path = doEditResolve(targetName); |
| 32 | + |
| 33 | + return; |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + % Check for targets elsewhere |
| 40 | + path = doEditResolve(name); |
| 41 | +end |
| 42 | + |
| 43 | +function path = doEditResolve (name) |
| 44 | + % Attempt to resolve the name in the same way that edit.m does |
| 45 | + |
| 46 | + [name, hasLocalFunction, result, ~, path] = matlab.internal.language.introspective.fixLocalFunctionCase(name); |
| 47 | + |
| 48 | + if hasLocalFunction |
| 49 | + % Handle a situation like `myFunc>localFunc` |
| 50 | + if result && path(end) == 'p' |
| 51 | + % See if a corresponding M file exists |
| 52 | + path(end) = 'm'; |
| 53 | + if ~isfile(path) |
| 54 | + path = ''; |
| 55 | + return; |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + if ~result |
| 60 | + path = ''; |
| 61 | + return; |
| 62 | + end |
| 63 | + else |
| 64 | + classResolver = matlab.internal.language.introspective.NameResolver(name, '', false); |
| 65 | + if isprop(classResolver, 'findBuiltins') |
| 66 | + classResolver.findBuiltins = false; |
| 67 | + end |
| 68 | + classResolver.executeResolve(); |
| 69 | + |
| 70 | + if isprop(classResolver, 'resolvedSymbol') |
| 71 | + resolvedSymbol = classResolver.resolvedSymbol; |
| 72 | + classInfo = resolvedSymbol.classInfo; |
| 73 | + whichTopic = resolvedSymbol.nameLocation; |
| 74 | + else |
| 75 | + classInfo = classResolver.classInfo; |
| 76 | + whichTopic = classResolver.nameLocation; |
| 77 | + end |
| 78 | + |
| 79 | + if isempty(whichTopic) |
| 80 | + [~, path] = resolveWithFileSystemAndExts(name); |
| 81 | + else |
| 82 | + % whichTopic is the full path to the resolved output either by class |
| 83 | + % inference or by which |
| 84 | + |
| 85 | + switch exist(whichTopic, 'file') |
| 86 | + case 0 % Name resolver found something which is not a file |
| 87 | + whichTopic = classInfo.definition; |
| 88 | + case 3 % MEX File |
| 89 | + path = ''; |
| 90 | + return |
| 91 | + case {4, 6} % P File or Simulink Model |
| 92 | + % See if a corresponding M file exists |
| 93 | + mTopic = regexprep(whichTopic, '\.\w+$', '.m'); |
| 94 | + if isfile(mTopic) |
| 95 | + whichTopic = mTopic; |
| 96 | + else |
| 97 | + path = ''; |
| 98 | + return |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + if matlab.desktop.editor.EditorUtils.isAbsolute(whichTopic) |
| 103 | + path = whichTopic; |
| 104 | + else |
| 105 | + path = which(whichTopic); |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | +end |
| 110 | + |
| 111 | +function result = hasExtension(s) |
| 112 | + % Helper method that determines if filename specified has an extension. |
| 113 | + % Returns 1 if filename does have an extension, 0 otherwise |
| 114 | + [~,~,ext] = fileparts(s); |
| 115 | + result = ~isempty(ext); |
| 116 | +end |
| 117 | + |
| 118 | +function [result, absPathname] = resolveWithFileSystemAndExts(argName) |
| 119 | + % Helper method that checks the filesystem for files by adding m or mlx |
| 120 | + result = 0; |
| 121 | + |
| 122 | + if ~hasExtension(argName) |
| 123 | + argMlx = [argName '.mlx']; |
| 124 | + [result, absPathname] = resolveWithDir(argMlx); |
| 125 | + |
| 126 | + if ~result |
| 127 | + argM = [argName '.m']; |
| 128 | + [result, absPathname] = resolveWithDir(argM); |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + if ~result |
| 133 | + absPathname = ''; |
| 134 | + end |
| 135 | +end |
| 136 | + |
| 137 | +function [result, absPathname] = resolveWithDir(argName) |
| 138 | + % Helper method that checks the filesystem for files |
| 139 | + result = 0; |
| 140 | + absPathname = ''; |
| 141 | + |
| 142 | + dir_result = dir(argName); |
| 143 | + |
| 144 | + if isempty(dir_result) && isSimpleFile(argName) |
| 145 | + dir_result = dir(fullfile('private', argName)); |
| 146 | + end |
| 147 | + |
| 148 | + if ~isempty(dir_result) |
| 149 | + if (numel(dir_result) == 1) && ~dir_result.isdir |
| 150 | + result = 1; % File exists |
| 151 | + absPathname = fullfile(dir_result.folder, dir_result.name); |
| 152 | + end |
| 153 | + end |
| 154 | +end |
| 155 | + |
| 156 | +function result = isSimpleFile(file) |
| 157 | + % Helper method that checks for directory seps. |
| 158 | + result = false; |
| 159 | + if isunix |
| 160 | + if ~contains(file, '/') |
| 161 | + result = true; |
| 162 | + end |
| 163 | + else % on windows be more restrictive |
| 164 | + if ~contains(file, ["\", "/", ":"]) % need to keep : for c: case |
| 165 | + result = true; |
| 166 | + end |
| 167 | + end |
| 168 | +end |
0 commit comments