Skip to content

Commit 79f5890

Browse files
committed
Introduce code analysis limit
1 parent d3444b1 commit 79f5890

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

matlab/+matlabls/+handlers/IndexingHandler.m

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ function handleDocumentIndexRequest (this, msg)
2525

2626
code = msg.code;
2727
filePath = msg.filePath;
28+
analysisLimit = msg.analysisLimit;
2829

29-
codeData = matlabls.internal.computeCodeData(code, filePath);
30+
codeData = matlabls.internal.computeCodeData(code, filePath, analysisLimit);
3031

3132
responseChannel = strcat(this.DocumentIndexingResponseChannel, '/', msg.channelId);
3233
matlabls.internal.CommunicationManager.publish(responseChannel, codeData)
@@ -36,9 +37,10 @@ function handleFolderIndexRequest (this, msg)
3637
% Indexes M-files the provided folders
3738

3839
folders = msg.folders;
40+
analysisLimit = msg.analysisLimit;
3941

4042
files = this.getAllMFilesToIndex(folders);
41-
this.parseFiles(msg.channelId, files)
43+
this.parseFiles(msg.channelId, files, analysisLimit)
4244
end
4345

4446
function filesToIndex = getAllMFilesToIndex (~, folders)
@@ -56,30 +58,30 @@ function handleFolderIndexRequest (this, msg)
5658
end
5759
end
5860

59-
function parseFiles (this, requestId, files)
61+
function parseFiles (this, requestId, files, analysisLimit)
6062
% Processes the given list of files and sends the data back to the language server.
6163

6264
if isMATLABReleaseOlderThan('R2021b')
6365
% If backgroundPool doesn't exist, leverage a timer to avoid blocking thread
64-
this.doParseFilesWithTimer(this, requestId, files);
66+
this.doParseFilesWithTimer(this, requestId, files, analysisLimit);
6567
else
66-
parfeval(backgroundPool, @this.doParseFiles, 0, requestId, files);
68+
parfeval(backgroundPool, @this.doParseFiles, 0, requestId, files, analysisLimit);
6769
end
6870
end
6971

70-
function doParseFilesWithTimer (this, requestId, files, index)
72+
function doParseFilesWithTimer (this, requestId, files, analysisLimit, index)
7173
% This leverages a timer to achieve an "asynchronous" looping effect, allowing
7274
% other operations to take place between parsing each file. This prevents the MATLAB®
7375
% thread from becomming blocked for an extended period of time.
7476

75-
if nargin == 3
77+
if nargin == 4
7678
index = 1;
7779
end
7880

7981
filePath = files(index);
8082
isLastFile = index == numel(files);
8183

82-
this.parseFile(requestId, filePath, isLastFile);
84+
this.parseFile(requestId, filePath, isLastFile, analysisLimit);
8385

8486
if ~isLastFile
8587
% More files - queue next file to parse
@@ -93,26 +95,26 @@ function timerCallback (t, ~)
9395
t.delete();
9496

9597
% Parse next file
96-
this.parseFiles(requestId, files, index + 1);
98+
this.doParseFilesWithTimer(requestId, files, analysisLimit, index + 1);
9799
end
98100
end
99101

100-
function doParseFiles (this, requestId, files)
102+
function doParseFiles (this, requestId, files, analysisLimit)
101103
% This can be executed in a separate thread (e.g. parfeval) to avoid blocking the
102104
% MATLAB thread.
103105

104106
for n = 1:numel(files)
105107
filePath = files{n};
106108
isLastFile = n == numel(files);
107-
this.parseFile(requestId, filePath, isLastFile);
109+
this.parseFile(requestId, filePath, isLastFile, analysisLimit);
108110
end
109111
end
110112

111-
function parseFile (this, requestId, filePath, isLastFile)
113+
function parseFile (this, requestId, filePath, isLastFile, analysisLimit)
112114
% Parses the given file and sends its data back to the language server
113115

114116
code = fileread(filePath);
115-
codeData = matlabls.internal.computeCodeData(code, filePath);
117+
codeData = matlabls.internal.computeCodeData(code, filePath, analysisLimit);
116118

117119
% Send data for this file
118120
msg.filePath = filePath;

matlab/+matlabls/+internal/computeCodeData.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function codeInfo = computeCodeData (code, filePath)
1+
function codeInfo = computeCodeData (code, filePath, analysisLimit)
22
%COMPUTECODEDATA Compute sub-function location information for the
33
% MATLAB® code file specified by fullpath. The data is a struct:
44
%
@@ -42,8 +42,7 @@
4242
functionReferences = {};
4343

4444
%% Handle very large input
45-
MAXCODE = 500000;
46-
if strlength(code) > MAXCODE
45+
if analysisLimit > 0 && strlength(code) > analysisLimit
4746
% File too large - do not try to index
4847
code = '';
4948
end

src/indexing/Indexer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import MatlabLifecycleManager from '../lifecycle/MatlabLifecycleManager'
77
import FileInfoIndex, { MatlabCodeData, RawCodeData } from './FileInfoIndex'
88
import * as fs from 'fs/promises'
99
import PathResolver from '../providers/navigation/PathResolver'
10+
import ConfigurationManager from '../lifecycle/ConfigurationManager'
1011

1112
interface WorkspaceFileIndexedResponse {
1213
isDone: boolean
@@ -69,9 +70,12 @@ export default class Indexer {
6970
FileInfoIndex.parseAndStoreCodeData(fileUri, fileResults.codeData)
7071
})
7172

73+
const analysisLimit = (await ConfigurationManager.getConfiguration()).codeAnalysisCharacterLimit
74+
7275
matlabConnection.publish(this.INDEX_FOLDERS_REQUEST_CHANNEL, {
7376
folders,
74-
channelId
77+
channelId,
78+
analysisLimit
7579
})
7680
}
7781

@@ -106,7 +110,7 @@ export default class Indexer {
106110
private async getCodeData (code: string, uri: string, matlabConnection: MatlabConnection): Promise<RawCodeData> {
107111
const filePath = URI.parse(uri).fsPath
108112

109-
return await new Promise(resolve => {
113+
return await new Promise(async resolve => {
110114
const channelId = matlabConnection.getChannelId()
111115
const channel = `${this.INDEX_DOCUMENT_RESPONSE_CHANNEL}/${channelId}`
112116
const responseSub = matlabConnection.subscribe(channel, message => {
@@ -115,10 +119,13 @@ export default class Indexer {
115119
resolve(message as RawCodeData)
116120
})
117121

122+
const analysisLimit = (await ConfigurationManager.getConfiguration()).codeAnalysisCharacterLimit
123+
118124
matlabConnection.publish(this.INDEX_DOCUMENT_REQUEST_CHANNEL, {
119125
code,
120126
filePath,
121-
channelId
127+
channelId,
128+
analysisLimit
122129
})
123130
})
124131
}

src/lifecycle/ConfigurationManager.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ interface Settings {
3333
matlabConnectionTiming: ConnectionTiming
3434
indexWorkspace: boolean
3535
telemetry: boolean
36+
codeAnalysisCharacterLimit: number
3637
}
3738

38-
type SettingName = 'installPath' | 'matlabConnectionTiming' | 'indexWorkspace' | 'telemetry'
39+
type SettingName = 'installPath' | 'matlabConnectionTiming' | 'indexWorkspace' | 'telemetry' | 'codeAnalysisCharacterLimit'
3940

4041
const SETTING_NAMES: SettingName[] = [
4142
'installPath',
4243
'matlabConnectionTiming',
4344
'indexWorkspace',
44-
'telemetry'
45+
'telemetry',
46+
'codeAnalysisCharacterLimit'
4547
]
4648

4749
class ConfigurationManager {
@@ -63,14 +65,16 @@ class ConfigurationManager {
6365
installPath: '',
6466
matlabConnectionTiming: ConnectionTiming.OnStart,
6567
indexWorkspace: false,
66-
telemetry: true
68+
telemetry: true,
69+
codeAnalysisCharacterLimit: 0
6770
}
6871

6972
this.globalSettings = {
7073
installPath: cliArgs[Argument.MatlabInstallationPath] ?? this.defaultConfiguration.installPath,
7174
matlabConnectionTiming: cliArgs[Argument.MatlabConnectionTiming] as ConnectionTiming ?? this.defaultConfiguration.matlabConnectionTiming,
7275
indexWorkspace: cliArgs[Argument.ShouldIndexWorkspace] ?? this.defaultConfiguration.indexWorkspace,
73-
telemetry: this.defaultConfiguration.telemetry
76+
telemetry: this.defaultConfiguration.telemetry,
77+
codeAnalysisCharacterLimit: this.defaultConfiguration.codeAnalysisCharacterLimit
7478
}
7579

7680
this.additionalArguments = {

src/providers/linting/LintingSupportProvider.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,16 @@ class LintingSupportProvider {
9595
const fileName = URI.parse(uri).fsPath
9696

9797
let lintData: string[] = []
98+
const code = textDocument.getText()
99+
100+
const analysisLimit = (await ConfigurationManager.getConfiguration()).codeAnalysisCharacterLimit
101+
if (analysisLimit > 0 && code.length > analysisLimit) {
102+
this.clearDiagnosticsForDocument(textDocument) // Clear document to handle setting changing value
103+
return
104+
}
98105

99106
if (isMatlabAvailable) {
100107
// Use MATLAB-based linting for better results and fixes
101-
const code = textDocument.getText()
102108
lintData = await this.getLintResultsFromMatlab(code, fileName, matlabConnection)
103109
} else {
104110
// Try to use mlint executable for basic linting

0 commit comments

Comments
 (0)