| 3887 | } |
| 3888 | |
| 3889 | void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const { |
| 3890 | String indent = _get_indentation(); |
| 3891 | |
| 3892 | Vector<String> lines = p_code.split("\n"); |
| 3893 | List<int> indent_stack; |
| 3894 | |
| 3895 | for (int i = 0; i < lines.size(); i++) { |
| 3896 | String l = lines[i]; |
| 3897 | int tc = 0; |
| 3898 | for (int j = 0; j < l.length(); j++) { |
| 3899 | if (l[j] == ' ' || l[j] == '\t') { |
| 3900 | tc++; |
| 3901 | } else { |
| 3902 | break; |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | String st = l.substr(tc).strip_edges(); |
| 3907 | if (st.is_empty() || st.begins_with("#")) { |
| 3908 | continue; //ignore! |
| 3909 | } |
| 3910 | |
| 3911 | int ilevel = 0; |
| 3912 | if (indent_stack.size()) { |
| 3913 | ilevel = indent_stack.back()->get(); |
| 3914 | } |
| 3915 | |
| 3916 | if (tc > ilevel) { |
| 3917 | indent_stack.push_back(tc); |
| 3918 | } else if (tc < ilevel) { |
| 3919 | while (indent_stack.size() && indent_stack.back()->get() > tc) { |
| 3920 | indent_stack.pop_back(); |
| 3921 | } |
| 3922 | |
| 3923 | if (indent_stack.size() && indent_stack.back()->get() != tc) { |
| 3924 | indent_stack.push_back(tc); // this is not right but gets the job done |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | if (i >= p_from_line) { |
| 3929 | l = indent.repeat(indent_stack.size()) + st; |
| 3930 | } else if (i > p_to_line) { |
| 3931 | break; |
| 3932 | } |
| 3933 | |
| 3934 | lines.write[i] = l; |
| 3935 | } |
| 3936 | |
| 3937 | p_code = ""; |
| 3938 | for (int i = 0; i < lines.size(); i++) { |
| 3939 | if (i > 0) { |
| 3940 | p_code += "\n"; |
| 3941 | } |
| 3942 | p_code += lines[i]; |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | #ifdef TOOLS_ENABLED |
no test coverage detected