Skip to content

Commit f354062

Browse files
committed
feat: added support for getSyntacticDiagnosticsSync, getSuggestionDiagnosticsSync, getNavBar, navTo, getNavTree, getNavTreeFull, and documentHighlights
1 parent 0d50bf9 commit f354062

File tree

3 files changed

+339
-6
lines changed

3 files changed

+339
-6
lines changed

src/exp.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,31 @@ for (let file of FILES) {
4747
for (const sample of file.getErrors.files) {
4848
await tsServer.openFile(sample);
4949
}
50-
//await tsServer.getErrors(file.getErrors.files, file.getErrors.delay);
50+
// await tsServer.getErrors(file.getErrors.files, file.getErrors.delay);
51+
//
52+
// await tsServer.getErrorsForProject(file.getErrorsForProject.filePath, file.getErrorsForProject.delay);
5153

52-
//await tsServer.getErrorsForProject(file.getErrorsForProject.filePath, file.getErrorsForProject.delay);
53-
54-
// await tsServer.openFile(file.getSemanticDiagnosticsSync.filePath);
54+
await tsServer.openFile(file.getSemanticDiagnosticsSync.filePath);
5555
const getSemanticDiagnosticsSyncResp = await tsServer.getSemanticDiagnosticsSync(file.getSemanticDiagnosticsSync.filePath, true);
5656
console.log('getSemanticDiagnosticsSyncResp', JSON.stringify(getSemanticDiagnosticsSyncResp));
57+
const getSyntacticDiagnosticsSyncResp = await tsServer.getSyntacticDiagnosticsSync(file.getSemanticDiagnosticsSync.filePath, true);
58+
console.log('getSyntacticDiagnosticsSyncResp', JSON.stringify(getSyntacticDiagnosticsSyncResp));
59+
const getSuggestionDiagnosticsSyncResp = await tsServer.getSuggestionDiagnosticsSync(file.getSuggestionDiagnosticsSync.filePath, true);
60+
console.log('getSuggestionDiagnosticsSyncResp', JSON.stringify(getSuggestionDiagnosticsSyncResp));
61+
62+
const getNavBarResp = await tsServer.getNavBar(file.getNavBar.filePath);
63+
console.log('getNavBarResp', JSON.stringify(getNavBarResp));
64+
65+
const navTorResp = await tsServer.navTo(file.navto.searchValue);
66+
console.log('navTorResp', JSON.stringify(navTorResp));
67+
68+
const getNavTreeResp = await tsServer.getNavTree(file.getNavTree.filePath);
69+
console.log('getNavTreeResp', JSON.stringify(getNavTreeResp));
70+
71+
const getNavTreeFullResp = await tsServer.getNavTree(file.getNavTreeFull.filePath);
72+
console.log('getNavTreeFullResp', JSON.stringify(getNavTreeFullResp));
73+
74+
const documentHighlightsResp = await tsServer.documentHighlights(file.documentHighlights.filePath, file.documentHighlights.line, file.documentHighlights.offset, file.documentHighlights.filesToSearch);
75+
console.log('documentHighlightsResp', JSON.stringify(documentHighlightsResp));
5776
}
5877
//tsServer.exitServer();

src/utils/server.js

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ function createTSServerInstance() {
621621
* to compute and return the diagnostics. The response is directly related to the current
622622
* state of the file at the time of the request.
623623
*/
624+
// TODO: Revisit find working usecase
624625
function getSemanticDiagnosticsSync(filePath, includeLinePosition = false) {
625626
const command = {
626627
command: "semanticDiagnosticsSync",
@@ -631,6 +632,284 @@ function createTSServerInstance() {
631632
};
632633
return sendCommand(command);
633634
}
635+
636+
/**
637+
* Sends a 'syntacticDiagnosticsSync' request to the TypeScript Server. This command is used
638+
* to synchronously obtain syntactic diagnostic information (like parsing errors) for a specified file.
639+
* Syntactic diagnostics are concerned with issues related to the parsing of the source code.
640+
* This function is particularly useful for quickly identifying syntax errors in a TypeScript file.
641+
*
642+
* @param {string} filePath - The path to the TypeScript file. The path should be absolute
643+
* or relative to the TypeScript server's current working directory.
644+
* @param {boolean} [includeLinePosition=false] - Specifies whether to include line and character
645+
* position information in the diagnostics. When set to true,
646+
* each diagnostic includes detailed position information,
647+
* which is useful for displaying errors directly in an editor.
648+
*
649+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver containing syntactic
650+
* diagnostics. The response is an array of diagnostic objects. Each diagnostic object
651+
* typically contains:
652+
* - `start`: The starting position of the diagnostic message.
653+
* - `length`: The length of the diagnostic message.
654+
* - `text`: The text of the diagnostic message.
655+
* If `includeLinePosition` is true, the diagnostic object also includes:
656+
* - `startLocation`: An object with line and character position of the start.
657+
* - `endLocation`: An object with line and character position of the end.
658+
*
659+
* Example usage:
660+
* ```
661+
* getSyntacticDiagnosticsSync('path/to/file.ts', true).then(response => {
662+
* console.log('Syntactic diagnostics:', response);
663+
* });
664+
* ```
665+
*/
666+
// TODO: Revisit find working usecase
667+
function getSyntacticDiagnosticsSync(filePath, includeLinePosition = false) {
668+
const command = {
669+
command: "syntacticDiagnosticsSync",
670+
arguments: {
671+
file: filePath,
672+
includeLinePosition: includeLinePosition
673+
}
674+
};
675+
return sendCommand(command);
676+
}
677+
678+
/**
679+
* Sends a 'suggestionDiagnosticsSync' request to the TypeScript Server. This command is used
680+
* to synchronously obtain suggestion diagnostic information for a specified file. Suggestion
681+
* diagnostics include tips and hints that may not necessarily be errors or warnings but could
682+
* suggest improvements or best practices in the code.
683+
*
684+
* @param {string} filePath - The path to the TypeScript file. This should be an absolute path
685+
* or relative to the TypeScript server's current working directory.
686+
* @param {boolean} [includeLinePosition=false] - Specifies whether to include line and character
687+
* position information in the diagnostics. When set to true,
688+
* each diagnostic includes detailed position information,
689+
* which is useful for displaying suggestions directly in an editor.
690+
*
691+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver containing
692+
* suggestion diagnostics. The response is typically an array of diagnostic
693+
* objects. Each diagnostic object includes:
694+
* - `start`: The starting position of the diagnostic message.
695+
* - `length`: The length of the diagnostic message.
696+
* - `text`: The text of the diagnostic message.
697+
* If `includeLinePosition` is true, the diagnostic object also includes:
698+
* - `startLocation`: An object with line and character position of the start.
699+
* - `endLocation`: An object with line and character position of the end.
700+
*
701+
* Example usage:
702+
* ```
703+
* getSuggestionDiagnosticsSync('path/to/file.ts', true).then(response => {
704+
* console.log('Suggestion diagnostics:', response);
705+
* });
706+
* ```
707+
* This function is particularly useful for tools and editors integrating TypeScript support,
708+
* providing an opportunity to present potential code improvements or best practices to the developer.
709+
*/
710+
// TODO: Revisit find working usecase
711+
function getSuggestionDiagnosticsSync(filePath, includeLinePosition = false) {
712+
const command = {
713+
command: "suggestionDiagnosticsSync",
714+
arguments: {
715+
file: filePath,
716+
includeLinePosition: includeLinePosition
717+
}
718+
};
719+
return sendCommand(command);
720+
}
721+
722+
/**
723+
* Sends a 'navbar' request to the TypeScript Server. This command is used to obtain
724+
* the navigation bar structure of a TypeScript file. The navigation bar typically
725+
* includes a hierarchical outline of the file's structure, including classes,
726+
* interfaces, functions, variables, and other code constructs.
727+
*
728+
* @param {string} filePath - The path to the TypeScript file for which the navigation
729+
* bar information is requested. The path should be absolute
730+
* or relative to the TypeScript server's current working directory.
731+
*
732+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver containing
733+
* the navigation bar information. The response is typically an array
734+
* of items representing the various code constructs in the file. Each item
735+
* includes:
736+
* - `text`: The name of the code construct (e.g., class name, function name).
737+
* - `kind`: The kind of code construct (e.g., 'class', 'function').
738+
* - `kindModifiers`: Modifiers applied to the code construct (e.g., 'public', 'static').
739+
* - `spans`: An array of span objects indicating the location of the construct in the file.
740+
* - `childItems`: An array of child items, following the same structure, representing nested constructs.
741+
*
742+
* Example usage:
743+
* ```
744+
* getNavBar('path/to/file.ts').then(response => {
745+
* console.log('Navigation bar structure:', response);
746+
* });
747+
* ```
748+
* This function is particularly useful for tools and editors integrating TypeScript support,
749+
* providing an opportunity to present a structured outline or overview of a code file to the developer.
750+
*/
751+
function getNavBar(filePath) {
752+
const command = {
753+
command: "navbar",
754+
arguments: {
755+
file: filePath
756+
}
757+
};
758+
return sendCommand(command);
759+
}
760+
761+
/**
762+
* Sends a 'navto' request to the TypeScript Server. This command is used for
763+
* searching named symbols in the project or in a particular file, with options
764+
* to limit the results and scope the search to a specific project.
765+
*
766+
* @param {string} searchValue - The search term to navigate to from the current location.
767+
* The term can be '.*' or an identifier prefix.
768+
* @param {string} [file] - Optional file path to restrict the search to a specific file.
769+
* @param {boolean} [currentFileOnly=false] - When true, limits search to the current file.
770+
* @param {number} [maxResultCount] - Optional limit on the number of items to return.
771+
* @param {string} [projectFileName] - Optional name of the project file (absolute pathname required).
772+
* @returns {Promise<Object[]>} A promise that resolves with an array of navigation items.
773+
*/
774+
function navTo(searchValue, file, currentFileOnly = false, maxResultCount, projectFileName) {
775+
const command = {
776+
command: "navto",
777+
arguments: {
778+
searchValue: searchValue,
779+
file: file,
780+
currentFileOnly: currentFileOnly,
781+
maxResultCount: maxResultCount,
782+
projectFileName: projectFileName
783+
}
784+
};
785+
return sendCommand(command);
786+
}
787+
788+
/**
789+
* Sends a 'navtree' request to the TypeScript Server to obtain the navigation tree of a TypeScript file.
790+
* The navigation tree provides a hierarchical outline of the file's contents, detailing classes, interfaces,
791+
* functions, variables, and other top-level constructs. This structured information is useful for
792+
* understanding the organization of code and for quick navigation within IDEs or editors.
793+
*
794+
* @param {string} filePath - The absolute path to the TypeScript file. This path is required
795+
* to locate the file within the project or file system.
796+
* @param {string} [projectFileName] - Optional. The absolute path to the project file (usually 'tsconfig.json').
797+
* Providing this path helps the TypeScript server correctly resolve the
798+
* file's context within a specific project, especially useful in workspaces
799+
* with multiple TypeScript projects.
800+
*
801+
* @returns {Promise<Object>} A promise that resolves with the navigation tree from the TypeScript server.
802+
* The tree is a hierarchical object with nodes representing various code constructs.
803+
* Each node typically includes:
804+
* - `text`: The name of the construct (e.g., class or function name).
805+
* - `kind`: The kind of construct (e.g., 'class', 'function').
806+
* - `spans`: Array of location spans indicating where the construct appears in the file.
807+
* - `childItems`: Array of child nodes for nested constructs (following the same structure).
808+
*
809+
* Example usage:
810+
* ```
811+
* getNavTree('path/to/file.ts', 'path/to/project.tsconfig.json').then(navTree => {
812+
* console.log('Navigation tree:', navTree);
813+
* });
814+
* ```
815+
* The returned navigation tree is especially valuable in development environments where a visual outline
816+
* or structure of the code file is beneficial for navigation and code comprehension.
817+
*/
818+
function getNavTree(filePath, projectFileName) {
819+
const command = {
820+
command: "navtree",
821+
arguments: {
822+
file: filePath,
823+
projectFileName: projectFileName
824+
}
825+
};
826+
return sendCommand(command);
827+
}
828+
829+
/**
830+
* Sends a 'navtree-full' request to the TypeScript Server. This command obtains a comprehensive
831+
* navigation tree of a TypeScript file, which provides a detailed outline of the file's structure.
832+
* The response includes an extensive hierarchy of all symbols and their nested scopes within the file,
833+
* such as classes, interfaces, functions, variables, and other code constructs.
834+
*
835+
* This detailed navigation tree is particularly useful for applications that require an in-depth
836+
* understanding of the file's structure, such as advanced IDE features for code navigation and analysis.
837+
*
838+
* @param {string} filePath - The absolute path to the TypeScript file for which the full navigation
839+
* tree is requested. This path is essential for the TypeScript server to locate
840+
* and analyze the file.
841+
*
842+
* @returns {Promise<Object>} A promise that resolves with the full navigation tree from the TypeScript server.
843+
* The tree is represented as an object with a hierarchical structure. Each node in the tree
844+
* includes:
845+
* - `text`: The name of the item (e.g., a class or function name).
846+
* - `kind`: The kind of item (e.g., 'class', 'function').
847+
* - `spans`: An array of span objects indicating the location of the item in the file.
848+
* - `childItems`: An array of child nodes representing nested declarations and structures.
849+
* Each child node follows the same structure.
850+
*
851+
* Example usage:
852+
* ```
853+
* getNavTreeFull('path/to/file.ts').then(navTreeFull => {
854+
* console.log('Full navigation tree:', navTreeFull);
855+
* });
856+
* ```
857+
*/
858+
function getNavTreeFull(filePath) {
859+
const command = {
860+
command: "navtree-full",
861+
arguments: {
862+
file: filePath
863+
}
864+
};
865+
return sendCommand(command);
866+
}
867+
868+
/**
869+
* Sends a 'documentHighlights' request to the TypeScript Server. This command is used to
870+
* obtain highlights of all occurrences of a symbol within a specified set of files. It is
871+
* particularly useful for identifying and navigating to instances of a variable, function name,
872+
* or other identifiers across multiple files.
873+
*
874+
* @param {string} filePath - The path to the TypeScript file where the symbol occurs.
875+
* @param {number} line - The line number where the symbol is located.
876+
* @param {number} offset - The character offset (position) in the line where the symbol is located.
877+
* @param {string[]} filesToSearch - The list of file paths to search for document highlights.
878+
* The search for symbol occurrences is conducted within these files.
879+
*
880+
* @returns {Promise<Object[]>} A promise that resolves with an array of document highlight objects.
881+
* Each object represents a file with highlight instances and includes:
882+
* - `file`: The file in which the highlight occurs.
883+
* - `highlightSpans`: An array of objects representing the highlight locations. Each object includes:
884+
* - `start`: The starting position of the highlight (line and character).
885+
* - `end`: The ending position of the highlight (line and character).
886+
* - `kind`: The kind of highlight (e.g., 'writtenReference', 'reference', 'definition').
887+
*
888+
* Example usage:
889+
* ```
890+
* documentHighlights('path/to/file.ts', 10, 5, ['path/to/file1.ts', 'path/to/file2.ts'])
891+
* .then(highlights => {
892+
* console.log('Document highlights:', highlights);
893+
* });
894+
* ```
895+
* This function is essential for features like symbol search in development environments,
896+
* where highlighting symbol occurrences enhances code understanding and navigation.
897+
*/
898+
//TODO: fix this for js use case
899+
function documentHighlights(filePath, line, offset, filesToSearch) {
900+
const command = {
901+
command: "documentHighlights",
902+
arguments: {
903+
file: filePath,
904+
line: line,
905+
offset: offset,
906+
filesToSearch: filesToSearch
907+
}
908+
};
909+
return sendCommand(command);
910+
}
911+
912+
634913
/**
635914
* Terminates the TypeScript Server process.
636915
* Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -666,6 +945,13 @@ function createTSServerInstance() {
666945
getErrors,
667946
getErrorsForProject,
668947
getSemanticDiagnosticsSync,
948+
getSyntacticDiagnosticsSync,
949+
getSuggestionDiagnosticsSync,
950+
getNavBar,
951+
navTo,
952+
getNavTree,
953+
getNavTreeFull,
954+
documentHighlights,
669955
exitServer
670956
};
671957
}

0 commit comments

Comments
 (0)